Java:读取文件并将其数据存储在ArrayList中?
问题描述:
我需要读取一个文件的格式如下:Java:读取文件并将其数据存储在ArrayList中?
[user1, user2, user3]
然后我需要将其存储在一个ArrayList。 我对您的帮助和抱歉,我的英语不好:)
答
好球员读取文件等..
由于不知道,我写了一些代码,我得到这个:
public static String getGroups(int index) {
try {
for (int i = 0; i < countLines(); i++) {
String[] content = new String[countLines()];
File file = new File(getIndexValues(i, 1));
try {
FileReader reader = new FileReader(file);
char[] chars = new char[(int) file.length()];
reader.read(chars);
content[i] = new String(chars);
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return content[index];
}
} catch (IOException ex) {
Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
它返回我想要的文件中的所有内容。我有一个文件列表,所以如果我输入getGroups(0)它将采取索引文件的第一行,并会寻找它的路径来加载它。
现在,我怎样才能把输出放入ArrayList? 我需要将它加载到JList中。
http://stackoverflow.com/questions/2788080/reading-a-text-file-in-java – DHerls 2015-02-23 15:56:50
嗨,欢迎来到SO!我建议你在询问之前抓取网页。你可以只用谷歌“java逐行阅读文件”等等。有很多教程+ API文档可以学习如何使用它。现在,你的问题有点广泛地回答了(你有什么困难?什么没有工作?等等)。 – 2015-02-23 15:58:04
我搜索,但无法使其工作。 该文件具有ArrayList格式。我只需要获取文件的所有内容并将其加载到ArrayList中。 我的想法是有一个像getUsers(字符串文件)的方法,当被调用时,返回一个ArrayList与所有的用户。那可能吗? – TheL0w3R 2015-02-23 16:01:17