3
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.

3

4 Answers 4

2

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);    
    }  
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

ÿþ 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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.