2016-12-27 5 views
0

複数のシリアル化を作成するにはどうすればよいですか?システムは最後のレコードをtxtに書き込みます。私は顧客情報を含むシンプルなDBを作成したい。そして、あなたが考えているのは、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. txt -fileは、テキストのみに使用されます。 ObjectOutputStreamは、複数のフィールドを格納します。クラス名(および存在する場合はserialVersionUID)を格納します。 .datデータベースまたはデータの一般的な拡張機能を使用してください。

  2. serializeCustomersここでCustomersが複数の場合、複数の顧客を保存できると考えられますが、複数の顧客を使用することはできません。代わりに、複数の顧客を保存するためにSet(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; 
      } 
関連する問題