Java如何将arrayList保存到文件中

时间:2014-04-01 02:24:11

标签: java arrays file arraylist

你好,我有这个代码,它要求用户输入一个视频和客户ID,并将其添加到名为hires的数组列表中,如何在完成后将该arraylist保存到文件并退出该程序?

    import java.util.ArrayList;
    import java.util.Scanner;


    public class Driver {
        public static final int LISTCUSTOMERS = 1;
        public static final int LISTVIDEOS = 2;
        public static final int LISTHIRES = 3;
        public static final int HIRESTATISTICS = 4;
        public static final int HIREVIDEO = 5;
        public static final int RETURNVIDEO = 6;
        public static final int QUIT = 7;
        public final static int DEFAULT = 0;
        private String[] menuText;
        private static final int MAXVIDEOS = 9;
        private static final int MAXCUSTOMERS = 5;
        static Scanner sc = new Scanner(System.in);
        private int selection;
        public int MAXLENGTH = 50;
        public static ArrayList<Hire>HireList = new ArrayList<Hire>();
        public static Video[]videos= new Video[MAXVIDEOS];
        public static Customer [] customers = new Customer[MAXCUSTOMERS];
        public static String [] options = {"","List Customers", "List Videos", "List Hires", "Hire Statistics", "Hire Video", "Return Video", "Quit"};
        public static Driver myMenu = new Driver (options, sc);
        public static boolean more = true;
        public static int option = 0;
        static Customer customer;

        public static void main (String [] args){
            initilisation();
            runHireVideo();
        }
        public Driver (String[] menuText, Scanner sc){
            this.menuText = menuText;
        }
            public int getMenuSelection(){
            this.displayMenu();
            this.selection = sc.nextInt();
                if(this.selection < DEFAULT || this.menuText.length<this.selection){
                    System.out.println("Your selection was invalid - using default");
                    this.selection = DEFAULT;
                }
            return this.selection;
        }
        public void displayMenu(){
            System.out.print("\nMicroVideo Menu: \n");
                for (int index = 1; index < menuText.length; index++)
                    System.out.println(index + ". "+ menuText [index]);
                System.out.print("Enter option: ");
        }
        public static void runHireVideo(){

            while (more){
                option = myMenu.getMenuSelection();
                switch(option){
                case LISTCUSTOMERS:
                    ListCustomers();
                break;
                case LISTVIDEOS:
                    ListVideos();
                break;
                case LISTHIRES:
                    ListHires();
                break;
                case HIRESTATISTICS:
                    HireStatistics();
                break;
                case HIREVIDEO:
                    HireVideo();
                break;
                case RETURNVIDEO:
                    ReturnVideo();
                break;
                default:
                    more = false;
                    System.out.println("Program Exited");
                break;
                }
            }
        }
        public static void initilisation(){
            videos[0]=new Video("140","The Matrix   ", 7.50,        3);
            videos[1]=new Video("141", "Terminator 2    ", 5.00,        3);
            videos[2]=new Video("142", "Shrek       ", 5.00,        10);
            videos[3]=new Video("143", "The Castle  ", 5.00,        1);
            videos[4]=new Video("146", "Sound Of Music  ", 1.00,        23);
            videos[5]=new Video("147", "Planet Of The Apes", 5.00,      0);
            videos[6]=new Video("148", "Mission Impossible", 1.00,      15);
            videos[7]=new Video("150", "Bagdad by Night", 6.00,         5);
            videos[8]=new Video("151", "Lord of the Rings 1", 5.00,         0);

            customers [0]= new Customer("9902JI", "Innes    ", 0,43484001);
            customers [1]= new Customer("8906RH", "Herbert", 0,43484000);
            customers [2]= new Customer("9012GT", "Turner   ", 0,43480009);
            customers [3]= new Customer("9012GS", "Sparke   ", 0,43480007);
            customers [4]= new Customer("9012MV", "Vallance", 0,43480008);
        }   
        public static void ListCustomers(){
            System.out.printf("%-6s  %-5s %6s\n", "Customer ID", "Customer Name", "     Customer Phone Number");
            for (int i=0;i<customers.length;i++){
            System.out.println(customers[i]);
            }
        }
        public static void ListVideos(){
            System.out.printf("%-8s  %-17s %6s %6s\n", "Video ID", "VideoName", "   Video Price Rate", "    In Stock");
            for (int i=0;i<videos.length;i++){
            System.out.println(videos[i]);
            }
        }
        public static void ListHires(){
            System.out.printf("%s   %s  %s  %s  %s \n","HireID", "Cust Name", "Video Name", "Days Hired", "Hire Cost");
            for (Hire hire: HireList){
                String Customer = customers[hire.getID(hire.getVideoID())].getCustomerName();
                String videoID = videos[hire.getID(hire.getVideoID())].getTitle();
                double DailyRate = videos[hire.getID(hire.getVideoID())].getRate()*hire.getDaysHired();
                System.out.printf("%d   %s  %s  %d  %1.2f \n",hire.getHireID(), Customer, videoID, hire.getDaysHired(), DailyRate);
            }
        }
        public static void HireStatistics(){
            System.out.printf("%s   %s  %s\n", "Video ID", "Video Name", "Days Hired");
                for (Hire hire: HireList){
                    String videoID = videos[hire.getID(hire.getVideoID())].getTitle();
                    System.out.printf("%s       %s  %d \n",hire.getVideoID(), videoID, hire.getDaysHired());
            }
        }
        public static void HireVideo(){
            int count =0;
            while (count == 0){
                int hireID = 1000+ HireList.size()+1;
                System.out.println("Enter Customer ID");
                String customerID = "9902JI";
                System.out.println("Enter Video ID");
                String videoID = "140";
                System.out.println("Enter Days Hired");
                int daysHired = 4;
                System.out.println("Enter Another Hire [Y/N]?");
                String Yes = sc.next();
                    if(Yes.equals("y")){
                    count = 0;
                }
                else if (Yes.equals("n")){ 
                    count = 1;
                }
                HireList.add(new Hire(hireID,customerID, videoID, daysHired, 0));
            }

        }
        public static void ReturnVideo(){
            System.out.println("Enter Customer ID");
            String customerID = "9902JI";
            System.out.println("Enter Video ID");
            String videoID = "140";

            for (int i = 0; i < HireList.size();i++){
                if (HireList.get(i).getCustomerID().equals(customerID)){
                    HireList.remove(i);
                }
            }
            for (int ii = 0; ii < HireList.size();ii++){
                    if (HireList.get(ii).getVideoID().equals(videoID)){
                        HireList.remove(ii);
                }
            }
            System.out.println("Removed " + customerID );
        }

    }

1 个答案:

答案 0 :(得分:1)

如前所述,最简单的方法是将每个成员保存在文本文件中,然后使用Scanner对象(http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html)将其读回。

如果您关心如何写入文本文件,我建议您阅读这篇文章:http://www.mkyong.com/java/how-to-write-to-file-in-java-bufferedwriter-example/ 它解释了如何使用FileWriter对象。

另一种选择是让Driver实现serialisable接口,允许您以一系列字节存储整个Driver对象,但这稍微复杂一些。关于序列化的一个体面的教程可以在这里找到: http://www.tutorialspoint.com/java/java_serialization.htm