美好的一天!我使用这样的代码部分
File file = new File(someFilePath);
Scanner sc;
try {
sc = new Scanner(file);
} catch (FileNotFoundException e) {
return "";
}
sc.useDelimiter("\Z");
System.out.println("file : " + file.getName() + " " + sc.hasNext() + " " + sc.delimiter());
String fileString = sc.next();
我在这段代码的最后一行收到错误Exception in thread "main" java.util.NoSuchElementException
。并且输出是 file : 758279215_profile.txt false Z
,所以分隔符是正确的,文件存在(它不是空的,我已经检查过了),但由于某种原因它没有下一个元素(我认为下一个元素应该是,它应该是文件中的整个文本)。出了什么问题以及如何解决?谢谢!
添加:
BufferedReader br = new BufferedReader(new FileReader(file));
String line = "";
while (line != null) {
try {
line = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(line);
}
返回文件的内容(内容编辑为 JSON 文本的文本文件)和 null(循环的最后一次迭代)
这可能是
区域设置问题。试试export LC_ALL=en_US.utf-8