384BufferedOutputStream字节缓中输出流和385 BufferedInputStream 字节缓)冲输入流

BufferedOutputStream--字节缓冲输出流

/*
java.io.BufferedOutputStream extends OutStream
BufferedOutputStream :字节缓冲输出流
继承父类的共同成员方法:
 方法:
close() 关闭此输出流并释放此流有关的所有系统资源
flush() 刷新次输出流并强制写出所有缓冲的输出字节
write() 有3个  将数据写入到硬盘中
  write(byte[] b)将b.length个字节从指定的byte数组写入此输出流
   write(int b)将指定的字节输出流  写一个字节
  write(byte[] b,int off,int len ) 将指定的字节数组写入len个字节,从偏移量off开始输出到此的输出流
  构造方法:
   BufferedOutputStream(OutputStream out) 创建一个新的缓冲输出流,以将数据写入指定的底层输出流
   BufferedOutputStream(OutputStream out,int size) 创建一个新的缓冲流,以将具有指定缓冲区大小的数据写入指定的底层输出流
   参数:
   OutputStream out:字节输出流 我们可以传递FileOutputStream,缓冲流会给FileOutputStream增加一个缓冲区,提高FileOutputStream的写入效率
   int size:指定缓冲流 内部的大小,不指定是默认的
   步骤:
   1.创建一个FileOutputStream对象,构造方法中绑定要输出的目的地
   2.创建BufferedOutputStream对象,构造方法中传递FileOutputStream对象,提高FileOutputStream对象的效率
   3.使用BufferedOutputStream对象中的方法write,把数据写入到内部缓冲区
   4.使用BufferedOutputStream对象中的方法flush,把内部缓冲区的数据,刷新到文件中
   5.释放资源(会先刷新数据),第4步可以省略
 */
public class DemoBufferedOutStream {
    public static void main(String[] args) throws IOException {
        // 1.创建一个FileOutputStream对象,构造方法中绑定要输出的目的地
        FileOutputStream fos=new FileOutputStream("E:\\多线程\\aa.txt");
        //2.创建BufferedOutputStream对象,构造方法中传递FileOutputStream对象,提高FileOutputStream对象的效率
        BufferedOutputStream bos=new BufferedOutputStream(fos);
        //3.使用BufferedOutputStream对象中的方法write,把数据写入到内部缓冲区
        bos.write("我把数据写入到内部缓冲区".getBytes());//getByte()写多个字节
        //4.4.使用BufferedOutputStream对象中的方法flush,把内部缓冲区的数据,刷新到文件中
        bos.flush();
        //5.释放资源(会先刷新数据),第4步可以省略
        bos.close();
    }
 

 

BufferedInputStream--字节缓冲输入流

 
 public static void main(String[] args) throws IOException {
         //1.创建FileInputStream对象,构造方法中绑定要读取的数据源
        FileInputStream fis = new FileInputStream("D:\\b.txt");
        //2.创建BufferedInputStream对象,构造方法中传递FileInputStream对象,提高FileInputStream对象的读取效率
        BufferedInputStream bis = new BufferedInputStream(fis);
        //3.使用BufferedInputStream对象中的方法read,读取文件
        //int read()从输入流中读取数据的第一个字节。
       /* int len = 0;  记录每次读取的字节
        while ((len=bis.read())!=-1){
            System.out.println((char)len);
        }*/

        //int read(byte[] b)从输入流中读取一定数量的字节,并将其存储在缓冲区数组b中。
        byte[] bytes = new byte[1024];  //存储每次读取的数据
        int len=0;  //记录每次读取的有效字节个数
        while ((len=bis.read(bytes))!=-1){
            System.out.println(new String(bytes,0,len));
        }
        //4.释放资源
        bis.close();
    }
 
 
public class Demo02BufferedInputStream {
    public static void main(String[] args) throws IOException {
        //1.创建FileInputStream对象,构造方法中要绑定要读取的对象
        FileInputStream fis = new FileInputStream("day10_IO\\a.txt");
        //2.创建BufferedInputStream对象,构造方法中传递FileInputStream对象,提高FileInputStream对象的读取效率
        BufferedInputStream bis = new BufferedInputStream(fis);
        //3.使用BufferedInputStream对象中的发方法,读取文件
        //int read() 从输入流中读取数据的下一个字节
        /*int len = 0; // 记录每次读取到的字节
        while((len = bis.read()) != -1){
            System.out.println( len);
        }*/

        //int read(byte[] b) 从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中
        byte[] bytes = new byte[1024];//存储每次读取的数据
        int len = 0; //记录每次读取到的字节
        while((len = bis.read(bytes)) != -1){
            System.out.println(new String(bytes,0,len));
        }
        //4.释放资源
        bis.close();
    }
}
 

 

posted @ 2022-07-13 10:21  ja不会va  阅读(23)  评论(0)    收藏  举报