ObjectInputStream.readObject()的问题!!!
import java.io.*;
import java.util.*;
public class ree
{
public static void main(String args[])
{
try{
File file = new File("e.txt");
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
Vector vector = new Vector();
String s = new String("wwww");
vector.add(s);
oos.writeObject(s);
oos.close();
fos.close();
}catch(IOException e){
System.out.println("IOException e");
}catch(ClassNotFoundException se){
System.out.println("ClassNotFoundException se");
}
}
}
//////////////////////////////////////////////////////////////////////
import java.io.*;
import java.util.*;
public class ree
{
public static void main(String args[])
{
try{
File file = new File("e.txt");
FileInputStream fos = new FileInputStream(file);
ObjectInputStream oos = new ObjectInputStream(fos);
Vector ob = (Vector)oos.readObject();
System.out.println(ob.toString());
oos.close();
fos.close();
}catch(IOException e){
System.out.println("IOException e");
}catch(ClassNotFoundException se){
System.out.println("ClassNotFoundException se");
}
}
}
////////////////////////////////////////////////////////////////////
class kk
{
int i;
String d;
}
/////////////
以上代码没问题,但改为
kk p = new kk();
p.i = 0;
p.d = "sd";
oos.writeObject(p);
////////////////////////////////
kk ob = (kk)oos.readObject();
////////////////////////////////
写入成功,无错误
但读的时候
kk ob = (kk)oos.readObject();
抛出 IOException 为什么???(写的时候都写入了) 如何才能读出一个 kk 类呢???