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就解决问题了。
本文介绍了一个关于使用Java的Scanner类从文件中读取中文字符时遇到的问题及解决方案。当文件编码为Unicode时,读取操作会失败,文章详细解释了如何将文件编码更改为UTF-8来解决此问题。
735

被折叠的 条评论
为什么被折叠?



