如何在不使用 BufferedReader
的情况下将文本文件的内容逐行读取到 String
中?
例如,我有一个文本文件,其内部如下所示:
Purlplemonkeys
greenGorilla
我想创建两个字符串
,然后使用类似的东西
File file = new File(System.getProperty("user.dir") + "\Textfile.txt");
String str = new String(file.nextLine());
String str2 = new String(file.nextLine());
这样,它就会为 str
分配 "Purlplemonkeys"
的值,并为 str2
分配 "greenGorilla"
的值>.
最佳答案
您可以读取文本文件来列出:
List<String> lst = Files.readAllLines(Paths.get("C:\\test.txt"));
然后根据需要访问每一行
附注文件 - java.nio.file.Files
关于java - 将文本文件逐行读取为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34208962/