0

I have the following code reading a file containing unicode text (Japanese).

File f = new File("f.txt"); // the file was saved with utf-8 encoding
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);

s = br.readLine();
lblData.setText(s); // JLabel lblData was set font as Tahoma

br.close();
fr.close();

Im using window 7 and the system already installed Japanese font (MSMINCHO.TTF).

If I run the above code in Netbeans (6.9) editor then the program display correctly.

But when I exported to jar file and run the program independently of Netbeans, then it is no longer displaying correctly.

I dont know why this happened and how to fix it?

ipkiss
  • 13,311
  • 33
  • 88
  • 123
  • why don't you use explicit localization to avoid dependencies like that? – tartar Apr 02 '12 at 06:52
  • Try setting the default encoding explicitly. This might be useful: http://stackoverflow.com/questions/361975/setting-the-default-java-character-encoding – Oleksi Apr 02 '12 at 06:56
  • [Java localization.](http://www.progdoc.de/papers/intSwing/intswing/intswing.html) – tartar Apr 02 '12 at 06:54

1 Answers1

1

Use FileInputStream and initialise the reader like this:

br = new BufferedReader(new InputStreamReader(new FileInputStream(filename), Charset.forName("UTF-8")));

that will give you the characters correct from the file.

Kennet
  • 5,736
  • 2
  • 25
  • 24