import java.io.*; class ObjectStreamDemo { public static void main(String[] args) throws IOException,ClassNotFoundException { writeObject(); readObject(); } public static void writeObject() throws IOException,ClassNotFoundException { ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("D:\\myfile\\mycode\\obj.txt")); oos.writeObject(new Person("zhangsan",23)); oos.writeObject(new Person("lisi",24)); oos.writeObject(new Person("wangwu",25)); oos.writeObject(new Person("zhaoliu",26)); oos.close(); } public static void readObject() throws IOException,ClassNotFoundException { ObjectInputStream ois=new ObjectInputStream(new FileInputStream("D:\\myfile\\mycode\\obj.txt")); Person p=(Person)ois.readObject(); sop(p); /* Person p=null; while((p=(Person)ois.readObject())!=null) { String str=p.toString(); sop(str); } */ ois.close(); } public static void sop(Object obj) { System.out.println(obj); } } class Person implements Serializable { private String name; private int age; Person(String name,int age) { this.name=name; this.age=age; } public String toString() { return "name:"+this.name+"...age:"+this.age; } }
/* 集合中涉及到io流的是Properties io流中涉及到线程的是管道流。 PipedInputStream PipedOutputStream PipedReader PipedWriter */ import java.io.*; class Read implements Runnable { private PipedInputStream in; Read(PipedInputStream in) { this.in=in; } public void run() { try { byte[] buf=new byte[1024]; System.out.println("读取前,没有数据阻塞"); int len=in.read(buf); System.out.println("读到数据,阻塞结束"); String s=new String(buf,0,len); System.out.println(s); in.close(); } catch (IOException e) { throw new RuntimeException("管道读取流失败"); } } } class Write implements Runnable { private PipedOutputStream out; Write(PipedOutputStream out) { this.out=out; } public void run() { try { System.out.println("开始写入数据,正在等待"); Thread.sleep(8000); //抛出InterruptedException异常 out.write("Hello World".getBytes());//抛出IOException异常 out.close(); } catch (Exception e) { throw new RuntimeException("管道输出流失败"); } } } class PipedStreamDemo { public static void main(String[] args) throws IOException { pipedStreamDemo(); } public static void pipedStreamDemo() throws IOException { PipedInputStream in=new PipedInputStream(); PipedOutputStream out=new PipedOutputStream(); in.connect(out); Read r=new Read(in); Write w=new Write(out); new Thread(r).start(); new Thread(w).start(); } }
/* DataInputStream DataOutputStream 可以用于操作基本数据类型的数据流对象 */ import java.io.*; class DataStreamDemo { public static void main(String[] args) throws IOException { //writeData(); readData(); } public static void readData() throws IOException { DataInputStream dis=new DataInputStream(new FileInputStream("D:\\myfile\\mycode\\data.txt")); byte[] buf=new byte[5]; int len=dis.read(buf); String str=new String(buf,0,len); int num=dis.readInt(); boolean flag=dis.readBoolean(); double d=dis.readDouble(); sop("str:"+str); sop("num:"+num); sop("flag:"+flag); sop("double:"+d); } public static void writeData() throws IOException { DataOutputStream dos=new DataOutputStream(new FileOutputStream("D:\\myfile\\mycode\\data.txt")); dos.write("hello".getBytes()); dos.writeInt(123); dos.writeBoolean(true); dos.writeDouble(9876.5432); dos.close(); } public static void sop(Object obj) { System.out.println(obj); } }
/* 用于操作字节数组的流对象。 ByteArrayInputStream:在构造的时候需要接收数据源,而且数据源是一个字节数组。 ByteArrayOutputStream:在构造的时候不需要定义数据目的,因为该对象中已经封装了可变长度的字节数组。 这就是数据目的地。 因为这两个流操作的都是数组,并没有使用系统资源,所以不用抛出异常也不用使用close进行关闭。 注意只有一个方法在使用时需要抛出异常,即ByteArrayOutputStream类中的writeTO方法会抛出IOException。 流操作规律说明: 源设备:键盘(System.in),硬盘(FileStream),内存(ArrayStream) 目的设备:控制台(System.out),硬盘(FileStream),内存(ArrayStream) 用流的读写思想来操作数组 */ import java.io.*; class ByteArrayStream //代表内存 { public static void main(String[] args) { ByteArrayInputStream bais=new ByteArrayInputStream("ABCDEFGH".getBytes()); ByteArrayOutputStream baos=new ByteArrayOutputStream(); int ch; while((ch=bais.read())!=-1) { baos.write(ch); } System.out.println(baos.size()); System.out.println(baos.toString()); } }