为什么在我的特定情况下无法将arrayList对象写入文件并从文件中读取?

时间:2015-04-26 18:17:35

标签: java file-io arraylist fileinputstream fileoutputstream

说明 我的程序基本上是从用户读取输入。输入对象存储在arrayList中。 arrayList对象被写入文件。因为我想要打印出所有输入值,所以我创建了public List<PersonInfo> readFile()方法来从文件中读取这些对象。

问题: 如果我在提到的目录中打开文本文件,我看到没有保存数据,也就是说没有数据被读取。

虽然我已经创建了适当的方法,但是对象无法存储在文件中并从文件中读取的原因是什么。

代码:

我的问题主要涉及这个课程:

public void writeFile(List<PersonInfo> information) {

    try {
        FileOutputStream fos = new FileOutputStream("C:\\Users\\Documents\\NetBeansProjects\\BankFile3.txt");
        ObjectOutputStream os = new ObjectOutputStream(fos);
        os.writeObject(information);
        fos.close();
        os.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

public List<PersonInfo> readFile() {

    try {

        FileInputStream fis = new FileInputStream("C:\\Users\\Documents\\NetBeansProjects\\BankFile3.txt");
        ObjectInputStream is = new ObjectInputStream(fis);

        fis.close();
        is.close();

    //return readFile();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;

}

主要课程:

List<PersonInfo> info = new ArrayList<PersonInfo>();
            List<PersonInfo> info2 = new ArrayList<PersonInfo>();
            ReadWrite rw=new ReadWrite();
            rw.writeFile(info);
            info2=rw.readFile();
            while (true) {
                System.out.println("\n");
                System.out.println("1. Input personal info\n"
                        + "2. Print them out\n"
                        + "3. Transfer credits\n"
                        + "*************"
                        + "*************");
                option1 = input.nextInt();
                input.nextLine();
                switch (option1) {
                    case 1:
                        PersonInfo personInfo = new PersonInfo();
                        //take the input
                        System.out.println("Enter a name: ");
                        personInfo.setName(input.nextLine());

                        System.out.println("Give ID: ");
                        personInfo.setId(input.nextInt());
                        System.out.println("Input credit: ");
                        personInfo.setCredit(input.nextDouble());
         info.add(personInfo);
                        break;
                    case 2:
          System.out.println("");
                        System.out.println("Name\t\tID\t\tCredit");
                        for (PersonInfo pInfo : info) {
                            System.out.println(pInfo);
                        }
                        System.out.println("\t\t.............\n"
                                + "\t\t.............");
                        break;
                    case 3:
                        //transfer credit
                        System.out.println("To transfer credit between two persons enter 1");
                        System.out.println("To transfer credit within the same persons enter 2");
                        option2 = input.nextInt();
                        input.nextLine();
                        switch (option2) {
                            case 1:
                                System.out.println("Enter the ID of the person you want to withdraw amount from: ");
                                withdraw_id = input.nextInt();

                                System.out.println("Enter withdraw amount: ");
                                withdraw_amount = input.nextDouble();
                   System.out.println("Enter the ID of the person you want to deposit into: ");
                                dep_id = input.nextDouble();
                                //the amount has been withdrawn will be deposited
                         System.out.println("Done!\tTo print them out out choose option 2");
                                break;
                        }
                }

            }
      }

setObject的类:

public PersonInfo() {
    this.name = null;
    this.id = 0;
    this.credit = 0;
}

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

2 个答案:

答案 0 :(得分:0)

        List<PersonInfo> info = new ArrayList<PersonInfo>();
        >> After initialization no values are added to list(ie for info         list object). So list should be empty.
        ReadWrite rw=new ReadWrite();
        rw.writeFile(info);
        >> Just info object available without any values.
        info2=rw.readFile();
       >> returns null by default

答案 1 :(得分:0)

你可以使用类似的东西:

public void writeFile(List<PersonInfo> information) {

    try {
        FileOutputStream fos = new FileOutputStream("C:\\Users\\Documents\\NetBeansProjects\\BankFile3.txt");
        ObjectOutputStream os = new ObjectOutputStream(fos);
        os.writeObject(information);
        os.flush();
        fos.close();
        os.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

public List<PersonInfo> readFile() {
    List<PersonInfo> dataFromFile = null;
    try {

        FileInputStream fis = new FileInputStream("C:\\Users\\Documents\\NetBeansProjects\\BankFile3.txt");
        ObjectInputStream is = new ObjectInputStream(fis);
        dataFromFile = (List<PersonInfo>) is.readObject();
        fis.close();
        is.close();

        //return readFile();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return dataFromFile;

}

班级PesonInfo应如下所示:

 import java.io.Serializable;
 public class PersonInfo implements Serializable {
   ...
 }

我正在使用此代码运行它:

 new ObjectWriter().writeFile(Lists.newArrayList(new PersonInfo("kuku")));
 List<PersonInfo> personInfos = new ObjectWriter().readFile();
 System.out.println(personInfos);