java - BufferedReader gives missing characters -
so trying change format of text file has line numbers every couple of lines make cleaner , easier read. made simple program goes in , replaces of first 3 characters of line spaces, these 3 character spaces numbers can be. actual text doesn't start until few more spaces in. when , have end result printed out comes out diamond question mark in , i'm assuming result of missing characters. seems of missing characters apostrophe symbol. if let me know how fix appreciate :)
public class conversion { public static void main(string args[]) throws ioexception { bufferedreader scan = null; try { scan = new bufferedreader(new filereader(new file("c:\\users\\nasir\\desktop\\beowulftesting.txt"))); } catch (filenotfoundexception e) { system.out.println("failed read file"); } string finalversion = ""; string currline; while( (currline = scan.readline()) !=null){ if(currline.length()>3) currline = " "+ currline.substring(3); finalversion+=currline+"\n"; } scan.close(); system.out.println(finalversion); } }
- instead of using
filereader
, useinputstreamreader
correct text encoding. think strange characters appearing because you're reading file wrong encoding. by way, don't use
+=
strings in loop, have. instead, usestringbuilder
:stringbuilder finalversion = new stringbuilder(); string currline; while ((currline = scan.readline()) != null) { if (currline.length() > 3) { finalversion.append(" ").append(currline.substring(3)); } else { finalversion.append(currline); } finalversion.append('\n'); }
Comments
Post a Comment