2012-06-14 142 views
0

現在我想知道你們是否可以幫助我將ArrayList序列化到文件上。現在我正在從學校總結,但是我沒有真正的編碼問題。我所做的是一個GUI地址簿,當用戶添加一個地址時,它會被存儲到一個數組列表中並添加到組合框中。這就是我正在做的。我問的是如果你可以序列化Arraylists。如果可以的話,你們可以教我如何?或者至少給我一些教程?序列化列表列表

非常感謝。如果你們在回答之前需要查看代碼,請告訴我,我會展示它。再次,謝謝你。

好這裏是我的全部代碼:

import java.awt.EventQueue; 


public class Address_Book { 

    private JFrame frame; 
    private JTextField newName; 
    private JTextField newAddress; 
    private JTextField newPhoneAddress; 
    ArrayList<Book> test = new ArrayList<Book>(); 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        Address_Book window = new Address_Book(); 
        window.frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the application. 
    */ 
    public Address_Book() { 
     initialize(); 
    } 

    /** 
    * Initialize the contents of the frame. 
    */ 
    private void initialize() { 
     frame = new JFrame(); 
     frame.setBounds(100, 100, 371, 262); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().setLayout(null); 

     final JComboBox comboBox = new JComboBox(); 
     final DefaultComboBoxModel model = new DefaultComboBoxModel(); 

     newName = new JTextField(); 
     newName.setBounds(10, 29, 79, 20); 
     frame.getContentPane().add(newName); 
     newName.setColumns(10); 

     JLabel lbl1 = new JLabel("Enter New Name:"); 
     lbl1.setBounds(10, 11, 107, 14); 
     frame.getContentPane().add(lbl1); 

     JLabel lbl2 = new JLabel("Enter New Address:"); 
     lbl2.setBounds(110, 11, 107, 14); 
     frame.getContentPane().add(lbl2); 

     newAddress = new JTextField(); 
     newAddress.setColumns(10); 
     newAddress.setBounds(109, 29, 96, 20); 
     frame.getContentPane().add(newAddress); 

     newPhoneAddress = new JTextField(); 
     newPhoneAddress.setColumns(10); 
     newPhoneAddress.setBounds(215, 29, 130, 20); 
     frame.getContentPane().add(newPhoneAddress); 

     JLabel lbl3 = new JLabel("Enter New Phone number:"); 
     lbl3.setBounds(215, 11, 140, 14); 
     frame.getContentPane().add(lbl3); 

     JButton btnAddNewContact = new JButton("Add new contact"); 
     btnAddNewContact.addMouseListener(new MouseAdapter() { 
      @Override 
      public void mousePressed(MouseEvent arg0) { 
       test.add((new Book(newName.getText(), newAddress.getText(), newPhoneAddress.getText()))); 
       //mergesort.mergesort(test, 0, test.size() - 1); 
       model.removeAllElements(); 
       for(int i=0; i < test.size();i++){ 
        model.addElement(test.get(i).getContact()); 
       } 
       comboBox.setModel(model); 
       newName.setText(""); 
       newAddress.setText(""); 
       newPhoneAddress.setText(""); 
      } 
     }); 
     btnAddNewContact.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
      } 
     }); 
     btnAddNewContact.setBounds(10, 53, 335, 23); 
     frame.getContentPane().add(btnAddNewContact); 

     JLabel lbl4 = new JLabel("Current Contacts:"); 
     lbl4.setBounds(10, 87, 107, 14); 
     frame.getContentPane().add(lbl4); 

     comboBox.setModel(model); 
     comboBox.setBounds(10, 101, 335, 20); 
     comboBox.setSelectedIndex(test.size()-1); 
     frame.getContentPane().add(comboBox); 
    } 
} 

,這裏是我的對象:

public class Book implements Comparable { 
    private String flName, Address, pNumber; 

    public Book(String Name, String address, String phoneNumber){ 
     setFlName(Name); 
     setAddress(address); 
     setpNumber(phoneNumber); 
    } 

    public String getpNumber() { 
     return pNumber; 
    } 

    public void setpNumber(String pNumber) { 
     this.pNumber = pNumber; 
    } 

    public String getAddress() { 
     return Address; 
    } 

    public void setAddress(String address) { 
     Address = address; 
    } 

    public String getFlName() { 
     return flName; 
    } 

    public void setFlName(String flName) { 
     this.flName = flName; 
    } 

    public String getContact() { 
     return flName + " " + Address + " " + pNumber; 
    } 

    public int compareTo(Object c) { 
     Book testBook = (Book)c; 

     if (testBook.getFlName().compareTo(this.getFlName()) < 0){ 
      return(-1); 
     }else if(testBook.getFlName().compareTo(this.getFlName()) == 0){ 
      return(0); 
     }else{ 
      return(1); 
     } 
    } 

} 

現在,再次,我不確定如何着手做的ArrayList序列化。我已經使用過物體,但沒有使用Arraylists,所以我不知道它是否是相同的過程。我只是在尋求建議,或者關於使ArrayLists可序列化的一些很好的教程。

+0

顯示出來......如果你真的嘗試了一些東西,你會得到更好的迴應。 – duffymo

+0

這有幫助嗎? – Spartan

回答

4

ArrayList文檔here

所有已實現的接口: 序列化,Cloneable的,可迭代,收藏列表,RandomAccess的

所以,是的,它的序列化。在Java中使用其他任何流都很容易,只需看看ObjectOutputStreamObjectInputStream

+0

謝謝傑克。 – Spartan

4

使用ObjectInputStreamObjectOutputStream,是這樣的:

public static void saveArrayListToFile(ArrayList<Book> books, String filePath) 
{ 
    ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(filePath))); 
    oos.writeObject(books); 
    oos.close(); 
} 

// ... 

public static ArrayList<Book> loadArrayListFromFile(String filePath) 
{ 
    ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(filePath))); 
    try 
    { 
     return (ArrayList<Book>) ois.readObject(); 
    } 
    finally 
    { 
     ois.close(); 
    } 
} 

注意Book類必須實現Serializable接口。我沒有測試代碼,但它應該工作。您可能需要嘗試捕獲上述代碼中的任何異常。