0

Text.txt ファイルから Java の ArrayList にデータを追加したい。

ゲッターとセッターだけで POJO Employee クラスを作成しました。

public class Employee {
    private String name;
    private String designation;
    private String joiningDate;

    public Employee(String name, String designation, String joiningDate) {
        this.name = name;
        this.designation = designation;
        this.joiningDate = joiningDate;
    }

    public String getJoiningDate() {
        return joiningDate;
    }

    public void setJoiningDate(String joiningDate) {
        this.joiningDate = joiningDate;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDesignation() {
        return designation;
    }

    public void setDesignation(String designation) {
        this.designation = designation;
    }
}

そして、これは私のメインクラスです:

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

public class MainClass {
    public static void main(String[] args) throws FileNotFoundException {

        ArrayList<Employee> emplo = new ArrayList<Employee>();
        BufferedReader file = new BufferedReader(new FileReader("D://Test.txt"));

        try {
            StringBuilder builder = new StringBuilder();
            String line;

            while ((line = file.readLine()) != null) {
                String token[] = line.split("|");
                String name = token[0];
                String designation = token[1];
                String joiningDate = token[2];
                Employee emp = new Employee(name, designation, joiningDate);
                emplo.add(emp);

            }
            for (int i = 0; i < emplo.size(); i++) {
                System.out.println(emplo.get(i).getName() + " "
                        + emplo.get(i).getDesignation() + " "
                        + emplo.get(i).getJoiningDate());
            }

            file.close();
        } catch (Exception e) {
            // TODO: handle exception
        }

    }
}

テキストファイルデータ:

John|Smith|23

Rick|Samual|25

Ferry|Scoth|30

私が欲しいもの:

John Smith 23

Rick Samual 25

Ferry Scoth 30

これに関するどんな助けもかなりのものになるでしょう

4

2 に答える 2