Java IO流(2)

一、缓冲流概要

  java缓冲流本身不具IO功能,只是在别的流上加上缓冲提高效率,像是为别的流装上一种包装。当对文件或其他目标频繁读写或操作效率低,效能差。这时使用缓冲流能够更高效的读写信息。因为缓冲流先将数据缓存起来,然后一起写入或读取出来。所以说,缓冲流还是很重要的,在IO操作时记得加上缓冲流提升性能。

二、缓冲字符输入流与缓冲字符输出流

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;


public class BufferedReaderAndBufferedWriter {
    
    public static void main(String[] args){
        
        BufferedReaderAndBufferedWriter bufferedReaderAndBufferedWriter=new BufferedReaderAndBufferedWriter();
        bufferedReaderAndBufferedWriter.bufferedReaderExample();
        bufferedReaderAndBufferedWriter.bufferedWriterExample();
        
    }
    
    
    public void bufferedReaderExample(){
        //D盘下放入一个txt文档命名为hello
        File f=new File("D:"+File.separator+"hello.txt");
        
        FileReader fileReader=null;
        
        BufferedReader bufferedReader=null;
        try {
            fileReader=new FileReader(f);
            //将原始流包装进入缓冲流
            bufferedReader=new BufferedReader(fileReader);
            char [] data=new char[2048]; 
            bufferedReader.read(data);
        
            System.out.println(new String(data));
        } catch (Exception e) {
            
            e.printStackTrace();
            
        }finally{
            
            //关闭流
            try {
                fileReader.close();
                bufferedReader.close();
            } catch (IOException e) {
            
                e.printStackTrace();
            }
        }
    }
    
    public void bufferedWriterExample(){
        //D盘下放入一个txt文档命名为hello
        File f=new File("D:"+File.separator+"hello.txt");
        
        Writer writer=null;
        
        BufferedWriter bufferedWriter =null;
        try {
            writer=new FileWriter(f,true);
            //将原始流包装进入缓冲流
            bufferedWriter =new BufferedWriter(writer);
            bufferedWriter.write("写入缓冲流!");
            //记得刷新,否则会报错
            bufferedWriter.flush();
        } catch (Exception e) {
            
            e.printStackTrace();
            
        }finally{
            
            //关闭流
            try {
                writer.close();
                bufferedWriter.close();
            } catch (IOException e) {
            
                e.printStackTrace();
            }
        }
    }

}

三、缓冲字节输入流与缓冲字节输出流


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


 public class BufInputAndBufOutput {
    
    public static void main(String[] agrs){
        BufInputAndBufOutput bufInputAndBufOutput=new BufInputAndBufOutput();
        bufInputAndBufOutput.bufInputExample();
        bufInputAndBufOutput.bufOutputExample();
    }
    
    public void bufInputExample(){
        //D盘下放入一个txt文档命名为hello
        File f=new File("D:"+File.separator+"hello.txt");
        
        InputStream input=null;
        
        BufferedInputStream bufInput=null;
        try {
            input=new FileInputStream(f);
            //将原始流包装进入缓冲流
            bufInput=new BufferedInputStream(input);
            byte [] data=new byte[2048]; 
            bufInput.read(data);
            System.out.println(new String(data));
        } catch (Exception e) {
            
            e.printStackTrace();
            
        }finally{
            //关闭流
            try {
                input.close();
                bufInput.close();
            } catch (IOException e) {
            
                e.printStackTrace();
            }
        }
    }
    
    public void bufOutputExample(){
        //D盘下放入一个txt文档命名为hello
        File f=new File("D:"+File.separator+"hello.txt");
        
        OutputStream output=null;
        
        BufferedOutputStream bufOutput =null;
        try {
            output=new FileOutputStream(f,true);
            //将原始流包装进入缓冲流
            bufOutput =new BufferedOutputStream(output);
            bufOutput.write("写入缓冲流!".getBytes());
            //记得刷新,否则会报错
            bufOutput.flush();
        } catch (Exception e) {
            
            e.printStackTrace();
            
        }finally{
            
            //关闭流
            try {
                output.close();
                bufOutput.close();
            } catch (IOException e) {
            
                e.printStackTrace();
            }
        }
    }


}

有什么问题或者疑问请大家留言评论!

Java IO流(1)

你可能感兴趣的:(Java IO流(2))