0

I read lines from a .txt file into a String list. I show the text in a JTextPane. The encoding is fine when running from Eclipse or NetBeans, however if I create a jar, the encoding is not correct. The encoding of the file is UTF-8. Is there a way to solve this problem?

6
  • What encoding are you using? Commented May 4, 2014 at 21:01
  • 1
    How are you reading the file? Do you create a new InputStreamReader(myFileInputStream, "UTF-8") or are you creating a FileInputStream which uses the platform encoding? Commented May 4, 2014 at 21:05
  • I use b = new BufferedReader(new FileReader("foo.txt")). Commented May 4, 2014 at 21:07
  • have a look at Java FileReader encoding issue Commented May 4, 2014 at 21:09
  • proceed with what @MikeSamuel has suggested. Commented May 4, 2014 at 21:10

2 Answers 2

1

Your problem is probably that you're opening a reader using the platform encoding.

You should manually specify the encoding whenever you convert between bytes and characters. If you know that the appropriate encoding is UTF-8 you can open a file thus:

FileInputStream inputFile = new FileInputStream(myFile);
try {
  FileReader reader = new FileReader(inputFile, "UTF-8");
  // Maybe buffer reader and do something with it.
} finally {
  inputFile.close();
}

Libraries like Guava can make this whole process easier..

Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried to run your jar as

java -Dfile.encoding=utf-8 -jar xxx.jar

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.