Question

I have made a small java program in netbeans that's read a text file. When I run the program in my netbeans, everything goes fine. So I made an executable jar of my program, but when I run that jar I get wired characters when the program read the text file.

For example: I get "Céleste" but it has to be Céleste.

That's my code to read the file:

private void readFWFile(File file){
    try {
        FileReader fr = new FileReader(file); 
        BufferedReader br = new BufferedReader(fr); 
        String ligne; 
        while((ligne = br.readLine()) != null) { 
            System.out.println(ligne);
        } 

        fr.close();
    } catch (IOException ex) {
        Logger.getLogger(FWFileReader.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Was it helpful?

Solution

The FileReader class uses the "platform default character encoding" to decode bytes in the file into characters. It seems that your file is encoded in UTF-8, while the default encoding is something else on your system.

You can read the file in a specific encoding using InputStreamReader:

    Reader fr = new InputStreamReader(new FileInputStream(file), "UTF-8"); 

OTHER TIPS

This kind of output is caused by a mismatch somewhere - your file is encoded in UTF-8 but the console where you print the data expects a single-byte encoding such as Windows-1252.

You need to (a) ensure you read the file as UTF-8 and (b) ensure you write to the console using the encoding it expects.

FileReader always uses the platform default encoding when reading files. If this is UTF-8 then

  • your Java code reads the file as UTF-8 and sees Céleste
  • you then print out that data as UTF-8
    • in NetBeans the console clearly expects UTF-8 and displays the data correctly
    • outside NetBeans the console expects a single-byte encoding and displays the incorrect rendering.

Or if your default encoding is a single byte one then

  • your Java code reads the file as a single byte encoding and sees Céleste
  • you then print out that data as the same encoding
    • NetBeans treats the bytes you wrote as UTF-8 and displays Céleste
    • outside NetBeans you see the wrong data you originally read.

Use an InputStreamReader with a FileInputStream to ensure you read the data in the correct encoding, and make sure that when you print data to the console you do so using the encoding that the console expects.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top