Scanner不能读中文?

来源:互联网 发布:淘宝客微信还能做吗 编辑:程序博客网 时间:2024/05/13 09:05
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就解决问题了。

原创粉丝点击