nextLine()扫描程序java无法正常工作(可能因为Unicode)

时间:2014-04-04 02:48:32

标签: java unicode java.util.scanner

我在一个文件中有一个String,它应该使用Scanner类中的nextLine()方法以下列方式读入:

some_string = "All the staff in the operating room has been specifically trained with a theoretical and practical 20-hour course.\xe2\x80\xa9Results: The overall average incidence of adverse events reported was determined by 4.8%, is consistent with the expectations of the study protocol, and is at a lower level than the average median rate of international studies (8.9%).\n"

我按以下方式创建扫描仪对象:

 Scanner br = new Scanner(new File("location of my file"), "UTF-8");

然后我通过这样做得到下一行:

while (br.hasNextLine()) {
       System.out.println(br.nextLine());
}

我得到了:

>All the staff in the operating room has been specifically trained with a theoretical and practical 20-hour course.
>Results: The overall average incidence of adverse events reported was determined by 4.8%, is consistent with the expectations of the study protocol, and is at a lower level than the average median rate of international studies (8.9%).

当存在非ASCII字符时,似乎nextLine()失败。任何想法为什么会这样?

2 个答案:

答案 0 :(得分:2)

试试这个:

    Scanner scanner = new Scanner(new File("the file"), "UTF-8").useDelimiter("\n");

    while (scanner.hasNext())
        System.out.println(scanner.next());

答案 1 :(得分:0)

我现在正在解决这个问题,遗憾的是Scanner不能处理非ascii字符,因此当它到达非ascii字符时,它会在文件结束时起作用。这就是hasNext或hasNextLine返回false的原因! 您可以更改方法并使用BufferedReader读取文件。

BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
    System.out.println(line);
}