import java.io.FileNotFoundException;运行时:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
public class SerializeArrayList {
public static void main(String[] args) {
ArrayListal=new ArrayList ();
al.add(你好世界”);
al.add (你好世界”);
try {
FileOutputStream fileOut = new FileOutputStream(test”);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(al);
out.close();
fileOut.close();
System.out.println(\n序列化已成功完成...\n”);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
序列化成功完成...打开后创建的二进制文件的内容如下所示:
import java.io.FileInputStream;Runtime:
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
public class SerializeArrayList {
public static void main(String[] args) {
//read
ArrayList<字符串>arraylist= new ArrayList<字符串>();
try {
FileInputStream fileIn = new FileInputStream(test”);
ObjectInputStream ois = new ObjectInputStream(fileIn);
arraylist = (ArrayList) ois.readObject();
ois.close();
fileIn.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch block
e.printStackTrace();
}
System.out.println(读取数据: \n”);
for(String o:arraylist)
System.out.println(o);
}
}
读取数据:就是这样!不要犹豫,提出您的问题;
hello world
hello world
Please disable your ad blocker and refresh the window to use this website.