编程语言
首页 > 编程语言> > Java学习day37-缓冲流

Java学习day37-缓冲流

作者:互联网

一、处理流之一:缓冲流

1.为了提高数据读写速度,Java API提供了带缓冲功能的流类,在使用这些流类时,会创建一个内部缓冲区数组。(缓冲流就是先把数据缓冲到内存中,在内存中做io操作)

2.根据数据操作单位可以把缓冲流分为:

  ①BufferedInputStream 和BufferOutputStream

  ②BufferedReader 和 BufferedWriter

3.缓冲流要“套接”在相应的节点流上,对读写的数据提供了缓冲的功能,提高了读写的效率,同时增加了一些新的方法。

4.对于输出的缓冲流,写出的数据会先在内存中缓存,使用flush()将会使内存中的数据立刻写出。

 

二、缓冲字节流

package day18;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class Test {
    public static void main(String[] args){
        try {
//            Test.testBufferedInputStream();
//            Test.testBufferedOutputStream();
            Test.copyFile();
            
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    /**
     * 缓冲字节输入流
     * BufferedInputStream
     * @throws Exception 
     * */
    public static void testBufferedInputStream() throws Exception{
        //文件字节输入流对象
        FileInputStream in = new FileInputStream("F:\\testdemo\\demo\\src\\day18\\tt.txt");
        
        //把文件字节输入流放到缓冲字节输入流对象
        BufferedInputStream br = new BufferedInputStream(in);
        
        byte[] b = new byte[10];
        
        int len = 0;
        
        while((len = br.read(b)) != -1){
            System.out.println(new String(b,0,len));
        }
        
        //关闭流的时候,本着一个最晚开的最早关,依次关
        br.close();
        in.close();
    }
    
    
    /**
     * 缓存字节输出流
     * BufferedOutputStream
     * @throws Exception 
     * */
    public static void testBufferedOutputStream() throws Exception{
        //创建字节输出流对象
        FileOutputStream out = new FileOutputStream("F:\\testdemo\\demo\\src\\day18\\tt1.txt"); 
        
        //把字节输出流对象放到缓冲字节输出流中
        BufferedOutputStream bo = new BufferedOutputStream(out);
        
        String s = "hello world";
        
        bo.write(s.getBytes());//写到内存中
        
        bo.flush();//刷到硬盘上
        
        //关闭流的时候,本着一个最晚开的最早关,依次关
        bo.close();
        out.close();
    }
    
    /**
     * 缓冲流实现文件的复制
     * @throws Exception 
     * */
    public static void copyFile() throws Exception{
        //缓冲输入流
        BufferedInputStream br = new BufferedInputStream(new FileInputStream("F:\\testdemo\\demo\\src\\day18\\tt1.txt"));
        
        //缓冲输出流
        BufferedOutputStream bo = new BufferedOutputStream(new FileOutputStream("F:\\testdemo\\demo\\src\\day18\\tt2.txt"));
        
        byte[] b = new byte[1024];
        
        int len = 0;//设置一个每次读取到的数据的长度,直到br.read方法执行到最后(比如hello world,会执行到d的后面一位,这个时候返回值就是-1)
        
        while((len = br.read(b)) != -1){
            bo.write(b, 0, len);//写到内存中
        }
        
        bo.flush();//刷到硬盘
        
        bo.close();
        br.close();
    }
}

 

三、缓冲字符流

package day18;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;

/**
 * 缓冲字符流
 * 
 * */

public class Test1 {
    public static void main(String[] args){
        try {
//            Test1.testBufferedReader();
//            Test1.testBufferedWriter();
            Test1.copyFile();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    
    /**
     * 缓冲字符输入流
     * BufferedReader
     * @throws Exception 
     * */
    public static void testBufferedReader() throws Exception{
        FileReader r = new FileReader("F:\\testdemo\\demo\\src\\day18\\tt.txt");
        
        BufferedReader br = new BufferedReader(r);
        
        char[] c = new char[100];
        
        int len = 0;
        
        while((len = br.read(c)) != -1){//br.read(c)读到文件的最后一个字符的下一位,返回值就是-1
            System.out.println(new String(c, 0, len));
        }
        br.close();
        r.close();
    }
    
    
    /**
     *缓冲字符输出流
     *BuffredWriter
     * @throws Exception 
     * */
    public static void testBufferedWriter() throws Exception{
        FileWriter fw = new FileWriter("F:\\testdemo\\demo\\src\\day18\\tt3.txt");
        
        BufferedWriter bw = new BufferedWriter(fw);
        
        String s = "hello world!!!!!";
        
        bw.write(s);
        
        bw.flush();
        
        bw.close();
        fw.close();
    }
    
    
    /**
     * 缓冲字符流复制文件
     * @throws Exception 
     * */
    public static void copyFile() throws Exception{
        BufferedReader br = new BufferedReader(new FileReader("F:\\testdemo\\demo\\src\\day18\\tt3.txt"));
        
        BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\testdemo\\demo\\src\\day18\\tt4.txt"));
        
        char[] c = new char[100];
        
        int len = 0;
        
        while((len = br.read(c)) != -1){
            bw.write(c, 0, len);
        }
        
        bw.flush();
        
        bw.close();
        br.close();
    }
}

 

标签:Exception,Java,day37,len,br,缓冲,new,throws
来源: https://www.cnblogs.com/su-peng/p/12595180.html