码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-07-27 01:38:58      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:rate   txt   int   ring   flush   数据   pac   code   清空   

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太好学了,太简单了");
//            换行,相当于\r\n
            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 高效缓冲区

标签:rate   txt   int   ring   flush   数据   pac   code   清空   

原文地址:https://www.cnblogs.com/java-jiangtao-home/p/9374982.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!