package test;
import java.io.File;
import le.APHero;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
public class useFile{
public static void main(String[] args) {
File f=new File("d:\\Jproject\\流.txt");
APHero [] h=new APHero[10];
for(int i=0;i<h.length;i++)
{
h[i]=new APHero();
h[i].name="who?"+i;
h[i].hp=200+i;
}
try(
FileOutputStream fos=new FileOutputStream(f);
ObjectOutputStream oos=new ObjectOutputStream(fos);
FileInputStream fis=new FileInputStream(f);
ObjectInputStream ois=new ObjectInputStream(fis);
)
{
oos.writeObject(h);
APHero[] h2=(APHero[]) ois.readObject();
System.out.println("对比两者:\n");
for (APHero apHero : h) {
System.out.println(apHero.name);
}
for (APHero apHero : h2) {
System.out.println(apHero.name);
}
}catch (FileNotFoundException e) {
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
}
}
实现了import java.io.Serializable; 这一接口。。。。
|