你这些语句是连在一起的吗?
kk p = new kk();
p.i = 0;
p.d = "sd";
oos.writeObject(p);
怎么又:
oos.readObject();
oos 到底是一个什么对象?
没看懂。偶这里有一个 Sample,你看一下吧。import java.io.*;public class ObjIO implements Serializable
{
protected String field = "Hello, world."; public ObjIO(String param){
field = param;
} public void printit(){
System.out.println(field);
} public static void main(String args[]) throws Exception
{
// writing ...
ObjectOutputStream objOut
= new ObjectOutputStream(new FileOutputStream("c:\\1.dat"));
ObjIO obj = new ObjIO("1");
objOut.writeObject(obj);
objOut.close(); // reading ...
ObjectInputStream objIn
= new ObjectInputStream(new FileInputStream("c:\\1.dat"));
ObjIO another = (ObjIO)objIn.readObject();
another.printit();
objIn.close();
}
}
kk p = new kk();
p.i = 0;
p.d = "sd";
oos.writeObject(p);
怎么又:
oos.readObject();
oos 到底是一个什么对象?
没看懂。偶这里有一个 Sample,你看一下吧。import java.io.*;public class ObjIO implements Serializable
{
protected String field = "Hello, world."; public ObjIO(String param){
field = param;
} public void printit(){
System.out.println(field);
} public static void main(String args[]) throws Exception
{
// writing ...
ObjectOutputStream objOut
= new ObjectOutputStream(new FileOutputStream("c:\\1.dat"));
ObjIO obj = new ObjIO("1");
objOut.writeObject(obj);
objOut.close(); // reading ...
ObjectInputStream objIn
= new ObjectInputStream(new FileInputStream("c:\\1.dat"));
ObjIO another = (ObjIO)objIn.readObject();
another.printit();
objIn.close();
}
}
1.你第一个程序中写入的对象为String,而你第二个程序中读出的对象却为Vector.此时jdk会
有异常:java.lang.ClassCastException:java.lang.String。
2.第二个程序中要读出对象,所以要注意使程序能读出写入对象的文件:"e.txt",让e.txt在第
二个程序的根目录下。
3.关于类kk不能读出的问题,你看是不是上面的两个原因。但要注意的是要序列化的类必须要实现Serializable接口,例如这样:
class kk implements Serializable{
{
int i;
String d;}.