Java 序列化数组列表

Java 序列化数组列表,java,serialization,deserialization,Java,Serialization,Deserialization,现在我想知道你们能否帮我把ArrayList序列化到一个文件中。现在我正在学校做一个总结,但是我没有真正的编码问题。我制作的是一个GUI地址簿,当用户添加地址时,它将存储到arraylist中并添加到comboBox中。这就是我正在做的。我想问的是,是否可以序列化ArrayList。如果可以,你们能教我怎么做吗?或者至少给我一些教程 非常感谢你。若你们在回答之前需要查看代码,请告诉我,我会显示出来。再次感谢你 好的,这是我的全部代码: import java.awt.EventQueue;

现在我想知道你们能否帮我把ArrayList序列化到一个文件中。现在我正在学校做一个总结,但是我没有真正的编码问题。我制作的是一个GUI地址簿,当用户添加地址时,它将存储到arraylist中并添加到comboBox中。这就是我正在做的。我想问的是,是否可以序列化ArrayList。如果可以,你们能教我怎么做吗?或者至少给我一些教程

非常感谢你。若你们在回答之前需要查看代码,请告诉我,我会显示出来。再次感谢你

好的,这是我的全部代码:

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);
    }
}
导入java.awt.EventQueue;
公共类通讯录{
私有JFrame;
私有JTextField newName;
私有JTextField newAddress;
私有JTextField newPhoneAddress;
ArrayList测试=新的ArrayList();
/**
*启动应用程序。
*/
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
公开募捐{
试一试{
地址簿窗口=新地址簿();
window.frame.setVisible(true);
}捕获(例外e){
e、 printStackTrace();
}
}
});
}
/**
*创建应用程序。
*/
公共广播书{
初始化();
}
/**
*初始化框架的内容。
*/
私有void初始化(){
frame=新的JFrame();
框架.立根(100100371262);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
最终JComboBox组合框=新JComboBox();
最终DefaultComboxModel=新的DefaultComboxModel();
newName=newjtextfield();
newName.setBounds(10,29,79,20);
frame.getContentPane().add(newName);
newName.setColumns(10);
JLabel lbl1=新JLabel(“输入新名称:”);
lbl1.立根(10,11,107,14);
frame.getContentPane().add(lbl1);
JLabel lbl2=新JLabel(“输入新地址:”);
lbl2.立根(110,11,107,14);
frame.getContentPane().add(lbl2);
newAddress=newjtextfield();
newAddress.setColumns(10);
新地址.挫折(109,29,96,20);
frame.getContentPane().add(newAddress);
newPhoneAddress=new JTextField();
newPhoneAddress.setColumns(10);
newPhoneAddress.setBounds(215,29130,20);
frame.getContentPane().add(newPhoneAddress);
JLabel lbl3=新JLabel(“输入新电话号码:”);
lbl3.立根(215,11,140,14);
frame.getContentPane().add(lbl3);
JButton btnAddNewContact=新JButton(“添加新联系人”);
btnAddNewContact.addMouseListener(新的MouseAdapter(){
@凌驾
public void mousePressed(MouseEvent arg0){
test.add((新书(newName.getText(),newAddress.getText(),newPhoneAddress.getText()));
//mergesort.mergesort(test,0,test.size()-1);
model.removeAllElements();
对于(int i=0;i
这是我的目标:

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

}
public类书实现了可比较的{
私有字符串名称、地址、pNumber;
公用电话簿(字符串名称、字符串地址、字符串电话号码){
setFlName(名称);
设置地址(地址);
setpNumber(电话号码);
}
公共字符串getpNumber(){
返回pnnumber;
}
public void setpNumber(字符串pNumber){
this.pNumber=pNumber;
}
公共字符串getAddress(){
回信地址;
}
公共无效设置地址(字符串地址){
地址=地址;
}
公共字符串getFlName(){
返回flName;
}
public void setFlName(字符串flName){
this.flName=flName;
}  
公共字符串getContact(){
返回flName+“”+Address+“”+pNumber;
}
公共整数比较(对象c){
图书测试图书=(图书)c;
if(testBook.getFlName().compareTo(this.getFlName())<0){
返回(-1);
}else if(testBook.getFlName().compareTo(this.getFlName())==0){
返回(0);
}否则{
申报表(1);
}
}
}

现在,我再次不确定如何继续使ArrayList可序列化。我使用过对象,但没有使用ArrayList,所以我不知道这是否是同一个过程。我只是想征求一些关于如何使ArrayList可序列化的建议,或者一些很棒的教程。

来自
ArrayList
文档:

所有实现的接口: 可序列化、可克隆、可编辑、集合、列表、随机访问

所以是的,它是可序列化的。与使用Java中的任何其他流一样,这样做非常简单,只需看看和。

使用和,类似于:

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();
    }
}
公共静态void saveArrayList文件(ArrayList书籍,字符串文件路径)
{
ObjectOutputStream oos=新的ObjectOutputStrea