-1

I use Java to read content from file into a String. But String can't display some characters, like ć ş ę.

Here is my code:

FileInputStream in = new FileInputStream(f);
BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));

PrintStream out = new PrintStream(System.out, false, "UTF-8");
while(true) {

    String line = br.readLine();
    if(line == null)
        break;
    out.println(line);
}

How can I solve this problem?

Rafer
  • 1,170
  • 2
  • 10
  • 15
  • try this thread..http://stackoverflow.com/questions/13877187/read-special-characters-from-a-file-and-write-in-another-file-using-java – Mohit Sep 23 '14 at 12:20
  • It may just come from your console not displaying those characters properly. Try writing in a File and check if it is correct. – Jean Logeart Sep 23 '14 at 12:20
  • your file is not in `UTF-8` obviously, only you can determine what the actual encoding is and then use it. –  Sep 23 '14 at 12:32
  • @Jarrod Roberson the file is indeed in UTF-8. It's very strange. – Rafer Sep 23 '14 at 14:50
  • @vikeng21 It displays '??' with these symbols. – Rafer Sep 23 '14 at 14:51

1 Answers1

-1

Presumably the fact that this fails indicates that the file is not in UTF8 encoding. When you tell java it is, it believes you, and as that is wrong, things don't work.

There's no practical general-purpose way to detect the encoding of an arbitrary file, so your choices are to convert it using some external tool, or find out what encoding it actually is and tell Java (via the argument to BufferedStreamReader constructor).

soru
  • 5,464
  • 26
  • 30
  • The file is indeed in UTF8 encoding. I've checked it using 'file -I'. My system is Mac OS. – Rafer Sep 23 '14 at 14:53