java - nextLine() 扫描仪 java 无法正常工作(可能是因为 Unicode)

标签 java unicode java.util.scanner

我在文件中有一个字符串,应该按以下方式使用 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() 似乎会失败。有什么想法为什么会发生这种情况吗?

最佳答案

试试这个:

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

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

关于java - nextLine() 扫描仪 java 无法正常工作(可能是因为 Unicode),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22852601/

相关文章:

java - 扫描仪将字符串括在大括号中

PHP 5.3 找不到 normalizer_normalize()

java - 使用扫描仪读取文件时,为什么扫描仪必须在方法中?

java - Java从文件中扫描每个字符

java - 在 Java 中打印 OO 表达式树

c++ - 为什么QStringLiteral返回乱码字符串

ruby - Ruby 1.9 中的 Unicode 字符串

java - 有Java语言的Java解析器吗?

java - 转换对象的模式

Java:传入 "this"的副本?