有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

目标是将人、狗、鼠和猫字符串对象写入哺乳动物列表。然后把鸟送到观鸟者那里,把鱼送到鱼名单上

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

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

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

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) 个答案

  1. # 1 楼答案

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

    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()调用后新行仍在字符串中,则可能需要将其替换为子字符串

  2. # 2 楼答案

    您可以尝试将文件内容加载到字符串变量中,并使用换行符拆分到数组中。然后用逗号分开每一行。由于数据的结构类似于一个db表,所以u可以安全地假设每行上的索引0代表一个列表,1代表d next&;最后一天2人

  3. # 3 楼答案

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