Garbage characters when reading text files in Java

I have a Java call windows bat file that performs some processing and generates an output file

if ( filexists("output.txt") == true)    
{   String FileLine; 
    FileInputStream fstream = new FileInputStream("output.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(fstream));

    FileLine = br.readLine();
    fstream.close();
    filein.close();

}

The variable fileline contains 3 garbage characters at the beginning I also checked several other files in progam. No file has this problem, except that it is created with the runtime function 9087. As you can see, three garbage characters appear in the output file When I open it with Notepad, I can't see those junk characters

Please suggest

Solution

This happens because the file encoding is not mentioned when creating the FileInputStream Assuming that your file is UTF-8 encoded, you need to do this

new FileInputStream("output.txt,"UTF-8"));

Change the encoding according to the encoding of the file

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>