import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class Foo implements Serializable{
public int x,y;
public Foo(int x, int y){
this.x = x;
this.y = y;
}
private void writeObject(ObjectOutputStream s) throws IOException{
s.writeInt(x);
//s.writeInt(y);
}
private void readObject(ObjectInputStream s) throws IOException{
if(s.available()>0){
x = s.readInt();
y = s.readInt();
}
}
public static void main(String args[]) throws FileNotFoundException, IOException{
Foo foo = new Foo(10,30);
ObjectOutputStream s = new ObjectOutputStream(new FileOutputStream("my"));
foo.writeObject(s);
ObjectInputStream s1 = new ObjectInputStream(new FileInputStream("my"));
foo.readObject(s1);
System.out.println(foo.x);
System.out.println(foo.y);
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class Foo implements Serializable{
public int x,y;
public Foo(int x, int y){
this.x = x;
this.y = y;
}
private void writeObject(ObjectOutputStream s) throws IOException{
s.writeInt(x);
//s.writeInt(y);
}
private void readObject(ObjectInputStream s) throws IOException{
/* if(s.available()>0){}*/
x = s.readInt();
y = s.readInt();
}
public static void main(String args[]) throws FileNotFoundException, IOException{
Foo foo = new Foo(10,30);
ObjectOutputStream s = new ObjectOutputStream(new FileOutputStream("my"));
foo.writeObject(s);
ObjectInputStream s1 = new ObjectInputStream(new FileInputStream("my"));
foo.readObject(s1);
System.out.println(foo.x);
System.out.println(foo.y);
}
}
Exception in thread "main" java.io.EOFException
at java.io.DataInputStream.readInt(DataInputStream.java:375)
at java.io.ObjectInputStream$BlockDataInputStream.readInt(ObjectInputStream.java:2776)
at java.io.ObjectInputStream.readInt(ObjectInputStream.java:950)
at Foo.readObject(Foo.java:23)
at Foo.main(Foo.java:32)
分享到:
相关推荐
ObjectInputStream的用法---马克-to-win java视频的介绍对象输入字节流
ObjectInputStream 和 ObjectOutputStream 的作用是,对基本数据和对象进行序列化操作支持。本文给大家详细介绍了ObjectInputStream 和 ObjectOutputStream的相关知识,感兴趣的朋友一起学习吧
主要介绍了java序列化与ObjectOutputStream和ObjectInputStream的实例详解的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
JavaIO对象流 FileInputStream 与 ObjectInputStream 和 ObjectOutputStream 的组合 奥利弗·缪尔
static ObjectInputStream inTwo=null;static FileOutputStream outOne=null;static ObjectOutputStream outTwo=null;public StudentManager(){super("学生基本信息管理系统");Jnumber=new JTextField(10);Jname =...
ObjectInputStream savewan1 = new ObjectInputStream( new FileInputStream("savewan1.txt")); ObjectInputStream savewan2 = new ObjectInputStream( new FileInputStream("savewan2.txt")); ...
objectInputStream
ObjectInputStream input;//网络套接字输入流 ClientReceive recvThread; //建立菜单栏 JMenuBar jMenuBar = new JMenuBar(); //建立菜单组 JMenu operateMenu = new JMenu ("操作(O)"); //建立菜单项 ...
拿点积分啊,不然不给下东西. Java Object的一个学习源码.
ObjectInputStream save1 = new ObjectInputStream(new FileInputStream( "save1.txt")); dw = (java.util.List) save1.readObject(); save1.close(); System.out.println("***************************...
ObjectInputStream ois =new ObjectInputStream(is); // 2.获取输出流 OutputStream os = socket.getOutputStream(); ObjectOutputStream oos =new ObjectOutputStream(os); Choice c=new Choice(); c....
ObjectInputStream save = new ObjectInputStream(new FileInputStream( "savegly.txt")); gly = (Manager) save.readObject(); save.close(); /** * 创建系统菜单对像 */ Menu cx = new Menu()...
ObjectInputStream b = new ObjectInputStream(f); RSAPrivateKey prk = (RSAPrivateKey) b.readObject(); BigInteger d = prk.getPrivateExponent(); // 获取私钥参数及解密 BigInteger n = prk....
ObjectInputStream ois = new ObjectInputStream(fis); Documents docs = (Documents) ois.readObject(); System.out.println("****************"); ArrayList<Document> doc = docs.getAl(); for ...
在该方法中,首先通过FileInputStream和ObjectInputStream读取源文件对象,然后通过ObjectOutputStream将读取的对象写入备份文件中。这个简单的示例代码展示了如何使用Java序列化来实现一个文件备份工具的简单功能,...
程序使用了ObjectInPutStream和ObjectOutPutStream这两个文件输入输出流,利用这两个文件输入输出流,可以方便的直接将通讯记录对象直接写入文件中,另外为了方便添加和删除一条记录还使用了系统的对象数组类...
分析: 相应的反序列化需要使用的类是ObjectInputStream,反序列化步骤如下: 创建ObjectInputStream对象; 使用ObjectInputStream的readObject方法取出对象。 Java高级程序设计实战教程第五章-Java序列化机制全文
JavaAdvance复习题汇总 1.java语言提供处理不同类型流的类的包是(D) a)java.sql b) java.util c) java.math d) java.io 2.不属于java.io包中的接口的一项是(C) a)DataInput b) DataOutput c)...D.ObjectInputStream
ObjectInputStream ois = null; List<T> list = new ArrayList(); try { if(!file.exists()){ file.createNewFile();// 首次运行,文件不存在,需要自动创建 } fis = new FileInputStream(file); ...