私は、入力ファイルの行から値を読み込み、オブジェクトのコンストラクタのパラメータとしてそれらを使用したいです

首長は言いました:

たとえば人ケビン・スペイシー156人23/07/1979独身はいについて//私は氏名ケビン、姓・スペイシー、ID 156を持つ人物オブジェクトを作成する必要があります

tgallei:

私は必ずスペースを含むにも名前が読み取ることができることを確認することができますので、このユースケースのために私は、CSVファイルを使用します。

ファイルの行は次のようになります。

ケビン;スペイシー; 156;男; 23/07/1979;シングル;はい

このファイルには、行ずつ読み、人物オブジェクトに変換することができます。

public class App
{
    private static void main(String[] args)
    {
        File file = new File("file.csv");

        if (!file.canRead() || !file.isFile()) {
            System.exit(0);
        }

        BufferedReader in = null;
        List<Person> persons = new ArrayList<>();

        try {
            in = new BufferedReader(new FileReader(file));

            String row = null;
            while ((row = in.readLine()) != null) {
                String[] data = row.split(";");

                int id = Integer.parseInt(data[2]);
                String firstName = data[0];
                String lastName = data[1];

                //TODO read the remaining parameters

                persons.add(new Person(id, firstName, lastName));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    static class Person {
        private int id;
        private String firstName;
        private String lastName;

        public Person(int id, String firstName, String lastName) {
            // TODO add the remaining parameters 
            this.id = id;
            this.firstName = firstName;
            this.lastName = lastName;
        }
    }
}

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=28933&siteId=1