过滤字节流FilterInputStream和FilterOutputStream
BufferedInputStream和BufferedOutputStream 需要使用已存在的节点流来构造,提供了带缓冲的读写,提高了读写的效率
package com.tanlei.InputOutputStream; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class BufferedInputOutputDemo { public static void main(String[] args) { try { FileUtil1.copyFile(new File("C:\zhangsan\tanlei.txt"), new File("D:\zhangsan.txt")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class FileUtil1{ public static void copyFile(File src,File dst) throws IOException { FileInputStream fis=new FileInputStream(src); FileOutputStream fos=new FileOutputStream(dst); BufferedInputStream bis=new BufferedInputStream(fis); BufferedOutputStream bos=new BufferedOutputStream(fos); long time=System.currentTimeMillis(); int data=0;//用来保存实际读到的字节数 while ((data=bis.read())!=-1) { bos.write(data); } bis.close(); bos.close(); long time2=System.currentTimeMillis(); System.out.println("共花费+"+(time2-time)); } }
DataInputStream和DataOutputStream 基本数据类型进行操作
package com.tanlei.InputOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class DataInputOutputDemo { public static void main(String[] args) throws IOException { String name="tanlei"; int age=20; boolean flag=true; char sex='男'; double money=110.56; DataOutputStream dos=new DataOutputStream(new FileOutputStream("D:\aa.txt")); dos.writeUTF(name); dos.writeInt(age); dos.writeDouble(money); dos.writeChar(sex); dos.writeBoolean(flag); dos.close(); DataInputStream dis=new DataInputStream(new FileInputStream("D:\\aa.txt")); System.out.println(dis.readUTF()); System.out.println(dis.readInt()); System.out.println(dis.readDouble()); System.out.println(dis.readChar()); System.out.println(dis.readBoolean()); dis.close(); } }
总结:
提供带缓冲的读写