gpt4 book ai didi

java - 将文件内容拆分到 ArrayList 中

转载 作者:行者123 更新时间:2023-12-01 10:28:37 25 4
gpt4 key购买 nike

我有一个文件,我可以在其中读取其内容。现在我想将每个单独的行单独拆分为一个 arrayList 但无法成功。

这就是我目前所拥有的

try {
input = new FileInputStream(test);


byte testContent[] = new byte[(int) test.length()];
input.read(testContent);


String testFile = new String(testContent);
System.out.println(testFile);

}

文件内容示例如下,

0,0,5,13,​​9,1,0,0,0,0,13,15,10,15,5,0,0,3,15,2,0,11, 8,0,0,4,12,0,0,8,8,0,0,5,8,0
0,0,0,12,13,5,0,0,0,0,0,11,16,9,0,0,0,0,3,15,16,6,0,0,0, 7,15,16,16,2,0,0,0,0,1,1
0,0,0,4,15,12,0,0,0,0,3,16,15,14,0,0,0,0,8,13,8,16,0,0,0, 0,1,6,15,11,0,0,0,1,8,1
0,0,7,15,13,​​1,0,0,0,8,13,6,15,4,0,0,0,2,1,13,13,0,0,0,0, 0,2,15,11,1,0,0,0,0,0,1
0,0,0,1,11,0,0,0,0,0,0,7,8,0,0,0,0,0,1,13,6,2,2,0,0, 0,7,15,0,9,8,0,0,5,16,10,0,1

我希望上面的内容像这样的数组

[0,0,5,13,​​9,1,0,0,0,0,13,15,10,15,5,0,0,3,15,2,0,11 ,8,0,0,4,12,0,0,8,8,0,0,5,8,0][0,0,0,12,13,5,0,0,0,0,0,11,16,9,0,0,0,0,3,15,16,6,0, 0,0,7,15,16,16,2,0,0,0,0,1,1][0,0,0,4,15,12,0,0,0,0,3,16,15,14,0,0,0,0,8,13,8,16,0, 0,0,0,1,6,15,11,0,0,0,1,8,1][0,0,7,15,13,​​1,0,0,0,8,13,6,15,4,0,0,0,2,1,13,13,0,0, 0,0,0,2,15,11,1,0,0,0,0,0,1][0,0,0,1,11,0,0,0,0,0,0,7,8,0,0,0,0,0,1,13,6,2,2, 0,0,0,7,15,0,9,8,0,0,5,16,10,0,1]

预先感谢您的帮助

最佳答案

怎么样Files.readAllLines() :

List<String> lines = Files.readAllLines(new File(test).toPath());

如果您使用的是 Java 7,则需要此版本:

List<String> lines = Files.readAllLines(new File(test).toPath(), StandardCharsets.UTF_8);

关于java - 将文件内容拆分到 ArrayList 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35219216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com