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); } } 

  1. instead of using filereader, use inputstreamreader correct text encoding. think strange characters appearing because you're reading file wrong encoding.
  2. by way, don't use += strings in loop, have. instead, use stringbuilder:

    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

Popular posts from this blog

java - JavaFX 2 slider labelFormatter not being used -

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -