2016-12-27 87 views
0

如何創建多個序列化?現在系統只寫最後一條記錄到txt。我想創建簡單的數據庫,它將包含客戶信息。而你認爲,這種存儲數據的方法在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(); 
        } 
       } 

      } 
     } 

最後一個問題,如果我使用序列化,我將能夠編輯和刪除存儲的特定對象?

回答

1
  1. A txt -file僅用於純文本。 ObjectOutputStream比字段存儲更多:它存儲類名(和serialVersionUID,如果存在的話)。嘗試使用.dat什麼是數據庫或數據的通用擴展。

  2. 您打電話給方法serializeCustomers其中Customers是複數,讓我想我們可以存儲多個客戶,但該參數不允許使用多個客戶。相反,我建議,存儲一個集合(如LinkedHashSet)來存儲多個客戶,是的,您可以讀寫LinkedHashSet

1

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; 
      }