您的位置:首页 > 其它

Scanner不能读中文?

2013-10-27 20:09 281 查看
public static void main(String[] args) {
try {
File file = new File("d:\\temp.txt");
Scanner input = new Scanner(file);
System.out.println(input.hasNext());
PrintWriter output = new PrintWriter("d:\\result.txt");
while (input.hasNext()) {
StringBuffer sf = new StringBuffer(input.nextLine());
sf.reverse();
output.print(sf);
}
input.close();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
编码遇到的问题。发现scanner读取temp.txt文本,当里面有中文时,nextLine返回的总是false。解决:有人说是eclipse编码问题,但改后还是不行。最终发现是temp.txt的文本编码问题。win7给我用了Unicode,改为utf-8就解决问题了。
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: