1、IO库
代码
package WriteArchitectureFromZero.NetProgram;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
//用户传入客户信息到服务端处理:必须实现接口Serializable(标志性接口:因为不带人和方法)
class People implements Serializable{
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class IO {
public static void main(String[] args) {
try {
People client=new People();
client.setName("zhangsan");
ByteArrayOutputStream bos=new ByteArrayOutputStream();
ObjectOutputStream oos=new ObjectOutputStream(bos);//继承了outputstream,因此可以放入子类的实例;
oos.writeObject(client);
byte[] ores=bos.toByteArray();
ByteArrayInputStream bis=new ByteArrayInputStream(ores);
ObjectInputStream ois=new ObjectInputStream(bis);
People p=(People)ois.readObject();
System.out.println("系统已经受理用户"+p.name);
} catch (Exception e) {
e.printStackTrace();
}
}
}
输出
系统已经受理用户zhangsan