简体   繁体   English

使用JFileChooser打开txt文件并填充ArrayList

[英]Open txt file using JFileChooser and populate an ArrayList

I have a Person class with ID, name, address and state instance variables 我有一个具有ID,名称,地址和状态实例变量的Person类

  • The ID have no setter(mutator) method ID没有设置器(mutator)方法
  • name,address and state instance variables have both setter and getter methods -The txt file to read in from is comma delimited 名称,地址和状态实例变量同时具有setter和getter方法-要读取的txt文件以逗号分隔

I want to open a txt file using JFileChooser and then pupulate the Person class type ArrayList by reading in line. 我想使用JFileChooser打开一个txt文件,然后通过在线阅读来对Person类类型ArrayList进行化伪。 I get stacked to set the read in ID to the Person object here is the method to read in and poupulate the arraylist 我被堆叠以将读入ID设置为Person对象,这是读入并填充arraylist的方法

public static ArrayList<Person> load(String fileName) throws IOException {
    ArrayList<person> lines = null;
    BufferedReader reader;
    try {
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle("Load which file?");
        int result = chooser.showOpenDialog(null);
        if (result == JFileChooser.APPROVE_OPTION) {
            File file = chooser.getSelectedFile();
            if (file != null) {
                fileName = file.getCanonicalPath();
                reader
                        = new BufferedReader(new FileReader(fileName));
                lines = new ArrayList<>();

                String line = reader.readLine();
                String data[];
                while ((line = reader.readLine()) != null) {
                    data = line.split(",");
                    Person s = new Person();


                     s.setName(data[0]);
                     s.setAddress(data[1]);
                    s.setState(data[2]);

                    //I got stack how to set ID bcs it has no set method

                    s.getAddress();                                   
                    s.getName();
                    s.getState();

                    lines.addAll(Arrays.asList(s));
                }
                reader.close();
                return lines;
            }
        }

    } catch (FileNotFoundException exp) {
        exp.printStackTrace();
    } catch (IOException exp) {
        exp.printStackTrace();
    }
    return lines;
}

} }

If you have decided that you dont want to change the ID after it assign, then you can overload the class constructor like below 如果您决定在分配ID后不想更改ID,则可以像下面这样重载类构造函数

public Person(int id){
    this.id = id;
}

then inside your method do like 然后在你的方法里面做

data = line.split(",");
Person s = new Person(whatever_if_you_wanana_set);

You can not set ID if there isn't any setter or constructor that takes ID . 如果没有使用ID setter或构造方法,则无法设置ID Besides, why are you using 而且你为什么用

lines.addAll(Arrays.asList(s));

As s is an object of Person , you can simply call lines.add(s); 由于sPerson的对象,因此您可以简单地调用lines.add(s); to add person object to list. 将人员对象添加到列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM