本文介绍了将文本文件读入数组列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对 Java 真的很陌生,所以我在弄清楚这一点时遇到了一些麻烦.所以基本上我有一个看起来像这样的文本文件:
I really new to Java so I'm having some trouble figuring this out. So basically I have a text file that looks like this:
1:John:false
2:Bob:false
3:Audrey:false
如何从文本文件中为每一行创建一个 ArrayList?
How can I create an ArrayList from the text file for each line?
推荐答案
从文件中读取并将每一行添加到 arrayList 中.例如,请参阅代码.
Read from a file and add each line into arrayList. See the code for example.
public static void main(String[] args) {
ArrayList<String> arr = new ArrayList<String>();
try (BufferedReader br = new BufferedReader(new FileReader(<your_file_path>)))
{
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
arr.add(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
}
}
这篇关于将文本文件读入数组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!