是否有必要在循环时创建缓冲读取器,并全部读取单个行文件?它也一直不变

最佳答案

这是我的方法:

String txt = new Scanner(new File(path)).useDelimiter("\\Z").next();


这会将整个文件读入String。

让我为您分解一下:

String txt = // the resulting String is stored here
new Scanner( // Create a scanner to read the file
new File( "pathToFile" ) ) // pass a new file to the scanner to read
.useDelimiter("\\Z") // set the delimiter to the end of the file
.next(); // read the scanner until the next delimiter,
         // in this case it is the end of the file.

关于java - 用Java读取单行文件的最佳方法是什么,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3285762/

10-12 12:55
查看更多