java 中Scanner类 关于中文输出的问题

zhouchibocool1 2012-09-18 11:49:01
package My2012day02;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Csdn_Scanner {

/**
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
File file = new File("c:\\1.txt");
Scanner in = new Scanner(file);
while(in.hasNextLine()){
System.out.println(in.nextLine());
}

}

}

当1.txt文本文件中,都是全英文字符时,可以再控制台正常输出。
但是当文件中有中文字符时,控制台就不输出。这是怎么回事,请高手解答。
...全文
556 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
安特矮油 2012-09-18
  • 打赏
  • 举报
回复
没发现任何错误呀!!
licip 2012-09-18
  • 打赏
  • 举报
回复
应该可以吧。有报什么错没有。
xiars123 2012-09-18
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

Csdn_Scanner.java的字符集什么?
1.txt的字符集又是什么?
都改成utf8试试。。。
[/Quote]
看上去的确很像字符集引起的恶心错误,你可以:
Eclipse更改默认字符集 设置UTF-8
eclipse 中UTF-8设置
1.windows->Preferences 打开"首选项"对话框;

2.然后,general->Workspace,右 侧Text file encoding,选择Other,改变为UTF-8。
jdjnn 2012-09-18
  • 打赏
  • 举报
回复
可以正常输出啊,代码没有问题
ddplayer12 2012-09-18
  • 打赏
  • 举报
回复
有没有报错?报错信息是什么?还有你1.txt文件里的内容都是什么???
我刚运行了一下,没问题
zhaoming262350 2012-09-18
  • 打赏
  • 举报
回复
没有错误呀!!!!
瞧一瞧看一看 2012-09-18
  • 打赏
  • 举报
回复
我的中文和英文都可以正常运行啊
菖蒲老先生 2012-09-18
  • 打赏
  • 举报
回复
Csdn_Scanner.java的字符集什么?
1.txt的字符集又是什么?
都改成utf8试试。。。
安特矮油 2012-09-18
  • 打赏
  • 举报
回复

public static void main(String[] args) throws FileNotFoundException {
File file = new File("d:\\1.txt");
Scanner in = new Scanner(file);
while (in.hasNextLine()) {
System.out.println(in.nextLine());
}

}
// 打印结果
// 哈哈哈啊哈
// adsfsad
// 阿道夫

刚帮你又测试了一下,确实没问题
zhouchibocool1 2012-09-18
  • 打赏
  • 举报
回复
后台有输出吗?

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧