IO流BufferedReader------BufferedWriter 高效缓冲区

Posted java-jiangtao-home

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IO流BufferedReader------BufferedWriter 高效缓冲区相关的知识,希望对你有一定的参考价值。

package bufferedWriter;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class Demo1 {
    public static void main(String[] args) {
        wirter();
        
        
    }
    /**
     * 高效缓冲区
     */
    public static void wirter()
    {
        FileWriter fw = null;
        BufferedWriter bw = null;
        
        try {
            fw = new FileWriter("d.txt");
            //创建高效缓冲区的对象
            bw = new BufferedWriter(fw);
            //输出数据
            for(int i=0 ; i<10; i ++) {
            bw.write("java太好学了,太简单了");
//            换行,相当于

            bw.newLine();
            }
            //清空缓冲区
            bw.flush();
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(bw!=null) {
                try {
                    //关闭缓冲区的流,会自动关闭FIlewriter
                    bw.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}
package bufferedReader;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Demo1 {
    public static void main(String[] args) {
        reader();
        
    }
    
    public static void reader()
    {
        FileReader fr = null;
        
        BufferedReader br = null;
        
        try {
            fr = new FileReader("d.txt");
            
            br = new BufferedReader(fr);
            
            int num = -1;
            
            while((num=br.read()) != -1) {
                System.out.print((char)num);
            }
            
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch(IOException e) {
            e.printStackTrace();
        }
        
        finally {
            if(br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
        }
        
        
    }
}
package bufferedReader;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Demo2 {
    public static void main(String[] args) {
        reader();
        
    }
    
    public static void reader()
    {
        FileReader fr = null;
        
        BufferedReader br = null;
        
        try {
            fr = new FileReader("d.txt");
            
            br = new BufferedReader(fr);
            char [] chs = new char[1024];
            int num = -1;
            
            while((num=br.read(chs)) != -1) {
                System.out.print(new String(chs,0,num));
            }
            
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch(IOException e) {
            e.printStackTrace();
        }
        
        finally {
            if(br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
        }
        
        
    }
}
package bufferedReader;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Demo3 {
    public static void main(String[] args) {
        reader();
        
    }
    
    public static void reader()
    {
        FileReader fr = null;
        
        BufferedReader br = null;
        
        try {
            fr = new FileReader("d.txt");
            
            br = new BufferedReader(fr);
            
            String line = br.readLine();
            
            //下一行读取数据的时候,如果没有数据的话,会得到空,null;
            //使用高效输入流可以读取一行数据
            while((line = br.readLine()) != null)
            {
                System.out.println(line);
            }
            
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch(IOException e) {
            e.printStackTrace();
        }
        
        finally {
            if(br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
        }
        
        
    }
}

 

以上是关于IO流BufferedReader------BufferedWriter 高效缓冲区的主要内容,如果未能解决你的问题,请参考以下文章

IO流之IO流综述

IO异常--缓冲流--转换流--序列化流( IO流2 )

IO流(序列流)

java中的io流都有哪些

java IO流 IO流概述

IO流