Java 将对象从文本文件分离到Arraylist

Java 将对象从文本文件分离到Arraylist,java,arraylist,Java,Arraylist,我想知道你如何从一个像下面这样的文本文件中获取信息,并将它们写入单独的数组列表中 human, parrot, salmon dog, chicken, trout rat, flamingo, goldfish cat, cardinal, minnow 如你所见,上面有哺乳动物、鸟类和鱼类!所以我将这些数组列表声明为 ArrayList<String> mammalList = new ArrayList<String>(); ArrayList<String

我想知道你如何从一个像下面这样的文本文件中获取信息,并将它们写入单独的数组列表中

human, parrot, salmon
dog, chicken, trout
rat, flamingo, goldfish
cat, cardinal, minnow
如你所见,上面有哺乳动物、鸟类和鱼类!所以我将这些数组列表声明为

ArrayList<String> mammalList = new ArrayList<String>();
ArrayList<String> birdList = new ArrayList<String>();
ArrayList<String> fishList = new ArrayList<String>();
ArrayList=newArrayList();
ArrayList birdList=新的ArrayList();
ArrayList fishList=新的ArrayList();
目标是将人、狗、鼠和猫字符串对象写入哺乳动物列表。然后把鸟送到观鸟者那里,把鱼送到鱼名单上

我知道如何使用缓冲读取器读取单个数组列表中的文本文件,但如何将每种动物划分为各自的类别,我感到困惑。非常感谢你的帮助

----------------------------------------------------------------------------------------- 下面是在Freestyle 076的帮助下正确运行的完成代码:)

导入java.io.BufferedReader;
导入java.io.FileNotFoundException;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.ArrayList;
公共类testClass{
公共静态void main(字符串[]args){
ArrayList=新的ArrayList();
ArrayList birdList=新的ArrayList();
ArrayList fishList=新的ArrayList();
BufferedReader read=null;
试试{
read=新的BufferedReader(新的文件阅读器(“data/animal_data.txt”);
字符串str;
而((str=read.readLine())!=null){
String[]lineValues=str.split(“”;//将空格上的字符串拆分为数组
哺乳动物列表.add(lineValues[0]。子字符串(0,lineValues[0]。length()-1));//哺乳动物值,子字符串用于删除逗号
birdList.add(lineValues[1]。子字符串(0,lineValues[1]。length()-1));//bird value,再次使用子字符串删除逗号
fishList.add(lineValues[2]);//fish值,无需删除逗号
}
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}最后{
如果(读取!=null){
试一试{
read.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
用于(字符串列表:列表){
系统输出打印LN(mList);
}
}
}

它目前仅使用增强的for循环打印哺乳动物列表。

使用StringTokenizer以逗号分隔字符串。您知道哪个位置有哪个元素,所以只需将它们添加到相应的ArrayList。

您可以尝试将文件内容加载到字符串变量中,并使用换行符拆分到数组中。然后用逗号分割每一行。由于数据的结构类似于db表,因此u可以安全地假设每行上的索引0用于哺乳动物列表,1用于d next,2用于d last

您可以拆分空间中的每行,并将哺乳动物、鸟类和鱼类值分别附加到哺乳动物、鸟类和鱼类列表中

String sCurrentLine;

br = new BufferedReader(new FileReader("yourfilepath"));

while ((sCurrentLine = br.readLine()) != null) {
    String[] lineValues = sCurrentLine.split(" "); //split the string on spaces into array
    mammalList.add(lineValues[0].substring(0,lineValues[0].length() - 1)); //the mammal value, substring is to remove the comma
    birdList.add(lineValues[1].substring(0,lineValues[1].length() - 1)); //bird value, again substring to remove comma
    fishList.add(lineValues[2]); //fish value, no comma to remove
}
我当然假设ArrayList已经被实例化了

还要注意fish值(listValues[2]),我不记得BufferedReader是否为每个读取行删除新行字符('\n')。如果在readLine()调用后,新行仍在字符串中,则可能需要将其替换为子字符串
String sCurrentLine;

br = new BufferedReader(new FileReader("yourfilepath"));

while ((sCurrentLine = br.readLine()) != null) {
    String[] lineValues = sCurrentLine.split(" "); //split the string on spaces into array
    mammalList.add(lineValues[0].substring(0,lineValues[0].length() - 1)); //the mammal value, substring is to remove the comma
    birdList.add(lineValues[1].substring(0,lineValues[1].length() - 1)); //bird value, again substring to remove comma
    fishList.add(lineValues[2]); //fish value, no comma to remove
}