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?
Asked
Active
Viewed 222 times
0

Daniel
- 318
- 6
- 20
-
What encoding are you using? – Buhake Sindi May 04 '14 at 21:01
-
1How 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? – Mike Samuel May 04 '14 at 21:05
-
I use `b = new BufferedReader(new FileReader("foo.txt"))`. – Daniel May 04 '14 at 21:07
-
have a look at [Java FileReader encoding issue](http://stackoverflow.com/questions/696626/java-filereader-encoding-issue) – Braj May 04 '14 at 21:09
-
proceed with what @MikeSamuel has suggested. – Braj May 04 '14 at 21:10
-
Answer from @MikeSamuel works. If you will leave an answer instead of a comment, I will mark it as answer. – Daniel May 04 '14 at 21:18
2 Answers
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..

Community
- 1
- 1

Mike Samuel
- 118,113
- 30
- 216
- 245
0
Have you tried to run your jar as
java -Dfile.encoding=utf-8 -jar xxx.jar

Kostas Kryptos
- 4,081
- 2
- 23
- 24