Why am i getting ?? when i try to read ä character from a text file in java?

試著忘記壹切 提交于 2019-12-20 07:36:53

问题


I am trying to read text from a text file. There are some special characters like å,ä and ö. When i make a string and print out that string then i get ?? from these special characters. I am using the following code:

File fileDir = new File("files/myfile.txt");

BufferedReader br = new BufferedReader(new InputStreamReader(
                      new FileInputStream(fileDir), "UTF8"));

String strLine;
while ((strLine = br.readLine()) != null)   {
    System.out.println("strLine: "+strLine);
}

Can anybody tell me whats the problem. I want strLine to show and save å, ä and ö as they are in text file. Thanks in advance.


回答1:


The problem might not be with the file but with the console where you are trying to print. I suggest you follow the following steps

  1. Make sure the file you are reading is encoded in UTF-8.
  2. Make sure the console you are printing to has the proper encoding/charset to display these characters

Finally, this article Unicode - How to get characters right? is a must read.




回答2:


Check here for the lists of Java supported encodings

Most common single-byte encoding that includes non-ascii characters is ISO8859_1; maybe your file is that, and you should specifiy that encoding for your FileInputStream



来源:https://stackoverflow.com/questions/11801543/why-am-i-getting-when-i-try-to-read-%c3%a4-character-from-a-text-file-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!