简体   繁体   English

Java中的Unicode文件处理

[英]Unicode file handling in java

BufferedReader br=new BufferedReader( new InputStreamReader(new FileInputStream("g.txt"), "UTF-16"));
ArrayList<String> al=new ArrayList<String>();
String str;
while((str=br.readLine())!=null)
{
    al.add(str);
}
Iterator<String> it=al.iterator();
while(it.hasNext())
{
    str=it.next();
    System.out.println(str);
}

In this code I can read data from Unicode file properly and also able to insert into arrylist correctly but while I am trying to iterate the data It just print question marks only like ???? 在这段代码中,我可以正确地从Unicode文件中读取数据,也可以正确地插入到arrylist中,但是当我尝试迭代数据时,它仅打印问号,例如????。 ?? ?? ????, ?????, and so on can you help to solve this issu ????,????????等可以帮助您解决此问题

This question has been answered on StackOverflow already and it has to do with System.out.println() using the default system codepage, not the set locale. 这个问题已经在StackOverflow 上得到了解答 ,它与使用默认系统代码页而不是设置区域设置的System.out.println()有关。

To print text using correct character encoding, create a new instance of PrintStream and use that to print your lines of text: 要使用正确的字符编码来打印文本,请创建一个PrintStream的新实例,并使用它来打印文本行:

String text_containing_non_latin_characters = "...";
PrintStream ps = new PrintStream(System.out, true, "UTF-16");
ps.println(text_containing_non_latin_characters);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM