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

时间:2014-12-07 02:01:33

标签: java arraylist

我想知道如何从如下结构的文本文件中获取信息,并将它们写入单独的数组列表。

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>();

目标是将人类,狗,大鼠和猫串对象写入哺乳动物列表。然后是鸟类到鸟类列表,鱼类到鱼类列表。

我知道如何使用缓冲读取器将文本文件读取到单个阵列列表中,但我很难知道如何将每只动物分成各自的类别。非常感谢你的帮助!

----------------------------------------------- ------------------------------------------

这里是完成的代码,可以正常使用Freestyle076的帮助:)

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class testClass {

    public static void main(String[] args) {

        ArrayList<String> mammalList = new ArrayList<String>();
        ArrayList<String> birdList = new ArrayList<String>();
        ArrayList<String> fishList = new ArrayList<String>();

        BufferedReader read = null;

        try {   
            read = new BufferedReader(new FileReader("data/animal_data.txt"));
            String str;
            while ((str = read.readLine()) != null) {
                String[] lineValues = str.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
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (read != null) {
                try {
                    read.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        for(String mList:mammalList){
            System.out.println(mList);
        }


    }

}

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

3 个答案:

答案 0 :(得分:0)

使用StringTokenizer以逗号分隔String。您知道哪个位置具有哪个元素,因此只需将它们添加到相应的ArrayLists。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

你可以分开这些空间的每一行,并将哺乳动物,鸟类和鱼类的数值分别附加到哺乳动物,鸟类和鱼类名单上。

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
}

我当然假设arraylists已经被实例化了。

另外要注意鱼的价值(listValues [2]),我不记得BufferedReader是否为每条读取线删除了新的行字符(&#39; \ n&#39;)。如果新行在readLine()调用之后仍然在字符串中,则可能需要对其进行子字符串