Search code examples
character-encodingbufferedreader

BufferedReader readLine has null bytes unlike String() -- different encoding?


In the following I'm trying to get some parts of a line printed, however I'm getting alternating null characters ONLY from the Strings that are from the input file.

BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream( cmdLnArgs[0] )));
String s;
while ( (s = br.readLine()) != null ) {
    String[] columns = s.split( "\t" );
    String out = "/storage/37C4-1314/Music/" + columns[1] + "/" + columns[3] + "\n";
    System.out.print( out );
}

Why is the following happening?... [in fact I cannot copy/paste because of the nulls!]:

/storage/37C4-1314/Music/ S a m e O l e L o v e . m p 3

i.e.:

enter image description here

Even "+ new String(columns[3].getBytes("UTF-8"),"UTF-8")" doesn't work for me. Nor does this.


Solution

  • The following worked finally after trying SOOO many combinations:

        FileInputStream fis = new FileInputStream( cmdLnArgs[0] );
        InputStreamReader isr = new InputStreamReader(new FileInputStream( new File(cmdLnArgs[0]) ), "UTF-16");
        BufferedReader br = new BufferedReader( isr );