io之DataStream
程序员文章站
2022-05-28 16:52:41
...
package com.laien.io; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /** * 节点流:可以跟文件直接关联,如:FileOutputStream * 过滤流:用以包装节点流,如:DataOutputStream,BufferedOutputStream * * @author Administrator * */ public class DataStreamTest { public static void main(String[] args) throws IOException { DataOutputStream dos = new DataOutputStream(new BufferedOutputStream( new FileOutputStream("d:/data.txt"))); byte b = 3; int i = 12; char ch = 'c'; float f = 3.14f; // 写成二进制流 dos.writeByte(b); dos.writeInt(i); dos.writeChar(ch); dos.writeFloat(f); dos.close(); /***** 读数据 *********/ DataInputStream dis = new DataInputStream(new BufferedInputStream( new FileInputStream("d:/data.txt"))); //与写数据的顺序要一致 System.out.println(dis.readByte()); System.out.println(dis.readInt()); System.out.println(dis.readChar()); System.out.println(dis.readFloat()); dis.close(); } }
上一篇: jquery validate 常用配置
下一篇: Gson替换json中name的值