如何读取文件,然后将所有行添加到java中的字符串数组?
示例:
来自输入txt文件的内容:
line1fromtxt
line2fromtxt
line3fromtxt
输出:
lines = {"line1fromtxt", "line2fromtxt", "line3fromtxt"};
答案 0 :(得分:0)
您可以使用Files.readAllLines
获取List<String>
,然后使用toArray
将其转换为String[]
String[] lines = Files.readAllLines(Paths.get(filename)).toArray(new String[0]);
使用List而不是数组会更简单。
List<String> lines = Files.readAllLines(Paths.get(filename));