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 in = new ObjectInputStream(fileIn); // 反序列化对象 User deserializedUser = (User) in.readObject(); in.close(); fileIn.close(); System.out.println("Deserialized User..."); System....
Java序列化与ObjectOutputStream和ObjectInputStream的实例详解 Java序列化是指将Java对象转换为字节流的过程,以便将其存储或传输,而后可以将其恢复为原来的对象状态。Java提供了两种方式来实现序列化: ...
在Java I/O体系中,ObjectInputStream和ObjectOutputStream是两个关键类,它们提供了序列化和反序列化的功能,使得Java对象可以被写入到流中(例如磁盘或网络)并在之后重新读取。而FileInputStream则用于处理二进制...
ObjectInputStream savewan1 = new ObjectInputStream( new FileInputStream("savewan1.txt")); ObjectInputStream savewan2 = new ObjectInputStream( new FileInputStream("savewan2.txt")); ...
ObjectInputStream input;//网络套接字输入流 ClientReceive recvThread; //建立菜单栏 JMenuBar jMenuBar = new JMenuBar(); //建立菜单组 JMenu operateMenu = new JMenu ("操作(O)"); //建立菜单项 ...
拿点积分啊,不然不给下东西. Java Object的一个学习源码.
在Java编程语言中,ObjectInputStream和ObjectOutputStream是用于处理对象序列化和反序列化的类,它们是Java.io包的一部分。对象序列化是将Java对象转换为字节流的过程,而反序列化则是将字节流恢复为原来的Java对象...
objectInputStream = new ObjectInputStream(new FileInputStream("data.txt")); User user = (User) objectInputStream.readObject(); System.out.println(user.getAddress()); System.out.println(user....
ObjectInputStream oi = new ObjectInputStream(bi); obj = oi.readObject(); bi.close(); oi.close(); } catch (Exception e) { System.out.println("translation" + e.getMessage()); e.printStackTrace();...
objectInputStream = new ObjectInputStream(new FileInputStream(cachedFile)); user = (User) objectInputStream.readObject(); Log.d(TAG, "recover user: " + user); // 更新UI或执行其他操作 } catch ...
ObjectInputStream b = new ObjectInputStream(f); RSAPrivateKey prk = (RSAPrivateKey) b.readObject(); BigInteger d = prk.getPrivateExponent(); // 获取私钥参数及解密 BigInteger n = prk....
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 ois = new ObjectInputStream(fis)) { MyObject readObject = (MyObject) ois.readObject(); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } ``` 3. 序列化版本...
通过这个实验,学生将学习如何使用 Java 语言进行文件输入输出操作,掌握 ObjectInputStream 和 ObjectOutputStream 两个类的使用方法,并了解如何将数据写入磁盘文件中。 一、实验目的 本次实验的目的是通过对 ...
- **反序列化**:`ObjectInputStream`会调用类的`readObject(ObjectInputStream in)`方法来控制反序列化过程。 ##### 2.3 实现`Externalizable`接口,定义`readExternal`和`writeExternal`方法 如果一个类实现了`...
ObjectInputStream si = new ObjectInputStream(fi); Student stu = (Student) si.readObject(); si.close(); } catch (Exception e) { System.out.println(e); } ``` 此外,课程还涵盖了其他基本的字符流,如`...