在文件中编写ArrayList对象

时间:2016-06-25 16:37:58

标签: java file arraylist

我的目标是创建一个函数,在文件中写入帐户(我的类)类型的 Arraylist 。我编写了以下代码,但 ArrayList myList 只是在循环的第二次运行中添加了两次指定的详细信息..Plus为第二次运行它只是跳过< strong>名称字段输入 ac

public void builIT(){
    Scanner sc = new Scanner(System.in);
    ArrayList<Account> myList = new ArrayList<>();
    Account a = new Account();
    String name = null;
    String ac = null;
    String phone = null;
    String email = null;
    String password = null;
    double balance = 0;
    try{
        FileOutputStream fout = new FileOutputStream("c:\\Users\\srawa\\Desktop\\thisisit");
        ObjectOutputStream oos = new ObjectOutputStream(fout);
        for(int i=0;i<2;i++){                
            System.out.print("Name:");
                name=sc.nextLine();
            System.out.print("ACNO:");
                ac=sc.nextLine();
            System.out.print("Phone:");
                phone=sc.nextLine();
            System.out.print("Email:");
                email=sc.nextLine();
            System.out.print("Password:");
                password=sc.nextLine();
            System.out.print("Balance:");
                balance=sc.nextDouble();
            a.set(name,ac,phone,email,password,balance);
            System.out.println(a);
            myList.add(a);               
            System.out.println(myList);
        }

        oos.writeObject(myList);
        oos.close();
    }catch(Exception e){
        e.printStackTrace();
    }
    }

请帮忙。谢谢。 P.S.-我是Stackoverflow的新手。

0 个答案:

没有答案
相关问题