简体   繁体   中英

How create multiple serialization?

How create multiple serialization ? Now the system write just last record to txt . I want create simple DB that will contain customers info. And what you think, about this method of storing data in txt . ?

private static void WriteCustomers(){
            System.out.println("|______Registration module______|");
            try {

                System.out.println("First name: ");
                String firstName = reader.readLine();

                System.out.println("Last name: ");
                String lastName = reader.readLine();

                .....

                CustomerManagement obj = new CustomerManagement();
            CustomerManagementD customerManagementD = new CustomerManagementD();
                customerManagementD.setFirstName(firstName);
                customerManagementD.setLastName(lastName);

                .....

            obj.serializeCustomers(customerManagementD);
            }catch (IOException e){
                e.getMessage();
            }
        }
    public void serializeCustomers(CustomerManagementD customerManagementD) {

            FileOutputStream fout = null;
            ObjectOutputStream oos = null;

            try {

                fout = new FileOutputStream("CustomerManagement.txt");
                oos = new ObjectOutputStream(fout);
                oos.writeObject(customerManagementD);

                System.out.println("Done");

            } catch (Exception ex) {

                ex.printStackTrace();

            } finally {

                if (fout != null) {
                    try {
                        fout.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                if (oos != null) {
                    try {
                        oos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            }
        }

Last question, If I use serialization, I will be able to Edit and Remove stored particular objects?

  1. A txt -file is used for text-only. The ObjectOutputStream stores more than then fields: it stores the classname (and serialVersionUID if present). Try to use .dat what is a generic extension for databases or datas.

  2. You call the method serializeCustomers where Customers is plural and let me think we can store multiple Customer, but the parameter does not allow to use multiple customers. Instead, I suggest, to store a Set (like a LinkedHashSet ) to store multiple customers, and yes, you can read and write to the LinkedHashSet .

SOLUTION

    private static void WriteCustomers(List<CustomerManagementD> list){

                    try {

                        System.out.println("First name: ");
                        String firstName = reader.readLine();

                        System.out.println("Last name: ");
                        String lastName = reader.readLine();

                        .....

                        // serialize collection of customers


                            customerManagementDArraysList.add(new CustomerManagementD(
                                    customerID,firstName,lastName,email,contactNo));
                            ObjectOutputStream outStream = null;
                            try {
                                outStream = new ObjectOutputStream(new FileOutputStream(file));
                                for (CustomerManagementD p : list) {
                                    outStream.writeObject(p);
                                }

                            } catch (IOException ioException) {
                                System.err.println("Error opening file.");
                            } finally {
                                try {
                                    if (outStream != null)
                                        outStream.close();
                                } catch (IOException ioException) {
                                    System.err.println("Error closing file.");
                                }
                            }
                        }else if (finalcheck.equals("2")){
                            Adminswitch();
                        }
                        System.out.println("|______Customer was successfully saved______|\n   Press 'Enter' to continue...");
                        String absentinput = reader.readLine();
                            Adminswitch();

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

         private static ArrayList ViewCustomer(){
                try{
                    FileInputStream fis = new FileInputStream(file);
                    ObjectInputStream oos =new ObjectInputStream(fis);
                    ArrayList<CustomerManagementD> customerManagementDArraysList = new ArrayList<>();
                    try {
                        while (true) {
                            CustomerManagementD cmd = (CustomerManagementD) oos.readObject();
                            customerManagementDArraysList.add(cmd);
                        }
                    }catch (EOFException e){
                     e.getMessage();
                    }
                     {
                         while (file.canRead()){
                             for (CustomerManagementD cmd : customerManagementDArraysList) {
                                 System.out.println("Customer ID: " + cmd.getCustomerID() + 
" First Name: " + cmd.getFirstName() +
     " Last Name: " + cmd.getLastName()+....);

                             }
                             break;
                        }
                    }
                }catch (IOException e){
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();}

                return null;
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM