代码如下
import java.io.*;
import java.util.ArrayList;
public class BruteForce { public static void main (String args[]){
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("C:\\Users\\Fredo\\Workspaces\\MyEclipse 8.5\\AgendaProgram\\data.txt"));
ObjectInputStream ois = new ObjectInputStream(new FileInputStream ("C:\\Users\\Fredo\\Workspaces\\MyEclipse 8.5\\AgendaProgram\\data.txt"));
ArrayList<FileOperation> info = new ArrayList<FileOperation>();
// ois.defaultReadObject();
info = (ArrayList<FileOperation>) ois.readObject();
System.out.print("asd");
info.add(new FileOperation("asdf","adsf"));
oos.writeObject(info);
oos.flush();
oos.close();
ois.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}class FileOperation implements Serializable{
public String username;
public String password;
//构造函数
public FileOperation (String username,String password) {
this.username = username;
this.password = password;
}
}显示错误老是 java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2554)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1297)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
at BruteForce.main(BruteForce.java:14)
import java.io.*;
import java.util.ArrayList;
public class BruteForce { public static void main (String args[]){
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("C:\\Users\\Fredo\\Workspaces\\MyEclipse 8.5\\AgendaProgram\\data.txt"));
ObjectInputStream ois = new ObjectInputStream(new FileInputStream ("C:\\Users\\Fredo\\Workspaces\\MyEclipse 8.5\\AgendaProgram\\data.txt"));
ArrayList<FileOperation> info = new ArrayList<FileOperation>();
// ois.defaultReadObject();
info = (ArrayList<FileOperation>) ois.readObject();
System.out.print("asd");
info.add(new FileOperation("asdf","adsf"));
oos.writeObject(info);
oos.flush();
oos.close();
ois.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}class FileOperation implements Serializable{
public String username;
public String password;
//构造函数
public FileOperation (String username,String password) {
this.username = username;
this.password = password;
}
}显示错误老是 java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2554)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1297)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
at BruteForce.main(BruteForce.java:14)
new FileInputStream
read
close
new FileOutputStream
write
close
//是不是操作的是同一个文件引起的?!
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("C:\\Users\\Fredo\\Workspaces\\MyEclipse 8.5\\AgendaProgram\\data.txt"));
ObjectInputStream ois = new ObjectInputStream(new FileInputStream ("C:\\Users\\Fredo\\Workspaces\\MyEclipse 8.5\\AgendaProgram\\data.txt"));