簡體   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);
}

在這段代碼中,我可以正確地從Unicode文件中讀取數據,也可以正確地插入到arrylist中,但是當我嘗試迭代數據時,它僅打印問號,例如????。 ?? ????,????????等可以幫助您解決此問題

這個問題已經在StackOverflow 上得到了解答 ,它與使用默認系統代碼頁而不是設置區域設置的System.out.println()有關。

要使用正確的字符編碼來打印文本,請創建一個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