public class IoDemo09 {
public static void main(String[] args) {
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
try {
oos =new ObjectOutputStream( new BufferedOutputStream(new FileOutputStream("abc.txt")));
oos.writeChar('a');
oos.writeInt(6);
oos.writeDouble(Math.random());
oos.writeBoolean(true);
oos.writeUTF("数据操作流");
oos.flush();
//DataInputStream数据输入流,专门负责读取使用DataOutputStream输出的数据
ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream("abc.txt")));
//直接读取数据,注意读取的顺序要与写入的顺序一致,否则不能正确读取
System.out.println("char:"+ois.readChar());
System.out.println("int:"+ois.readInt());
System.out.println("double:"+ois.readDouble());
System.out.println("boolean:"+ois.readBoolean());
System.out.println("stirng:"+ois.readUTF());
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
try {
if(oos!=null) {
oos.close();
}
if(ois!=null) {
ois.close();
}
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
}
}
IO流—数据操作流
最新推荐文章于 2023-02-04 15:51:41 发布