我在文件中有一个字符串,应该按以下方式使用 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/