/*缓冲流:读取数据大量的文件时,读取的速度慢java提供了一套缓冲流 提高io流的效率 * */ package cn.lijun.demo; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; /* * 字节输入流的缓冲流 * 继承InputStream * 构造方法 */ public class BufferedInputStreamDemo { public static void main(String[] args) throws Exception { //创建字节输入流的缓冲流对象 BufferedInputStream bis=new BufferedInputStream(new FileInputStream("d:\\b.txt")); byte[] b=new byte[1024]; int len=0; while((len=bis.read(b))!=-1){ System.out.println(new String(b,0,len)); } bis.close(); } } package cn.lijun.demo; import java.io.BufferedOutputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; /* * 字节输出流的缓冲流 作用 提高效率 * 继承OutputStream * 构造方法new BufferedOutputStream(OutputStream out); */ public class BufferedOutputStreamDemo { public static void main(String[] args) throws Exception { FileOutputStream fos=new FileOutputStream("d:\\b.txt"); BufferedOutputStream bos=new BufferedOutputStream(fos); bos.write(65); byte[] b="hello world".getBytes(); bos.write(b); bos.write(b,3,2); //从索引为三的开始 截取两位数 bos.close(); } }
原文地址:https://www.cnblogs.com/qurui1998/p/10602710.html
时间: 2024-11-09 02:06:31