While reading from file in java, input is prefixed by two junk characters

Viewed 638
FileInputStream fin = new FileInputStream("D:\\testout.txt");    
BufferedInputStream bin = new BufferedInputStream(fin);    
int i;    
while((i = bin.read())!=-1) {    
    System.out.print((char)i);    
}    

bin.close();    
fin.close();    

output: ÿþGreat

I have checked the file testout.txt, it contains only one word i.e, Great.

4 Answers

When you're using text, you should use a Reader. eg.

try(
    BufferedReader reader = Files.newBufferedReader(
        Paths.get("D:\\testout.txt"), 
        StandardCharsets.UTF_8)
    ){
    int i;    
    while((i = reader.read())!=-1) {    
        System.out.print((char)i);    
    }  
}

That's most probably the Byte order mark, optional but allowed in files using UTF-8 character encoding. Some programs (e.g. Notepad) account for this possibility, some don't. Java by default doesn't strip them.

One utility to solve this is the BOMInputStream from Apache Commons IO.

Also, Notepad will write the byte order mark in the file when you save it as UTF-8.

ÿþ is the byte order mark in UTF-16. You can convert your string to UTF-8 with java.io as explained here.

You may also refer to the answer for more detail.

Please use utf-8 Characters encoding for resolving this kind of issue. byte[] utf_8 = input.getBytes("UTF-8"); // convert unicode string to UTF-8 String test = new String(utf_8);

Related