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

IO_BufferedReader&BufferedWriter

时间:2018-07-08 14:39:52      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:char   取数   buffered   tor   数据   部分   rjava   []   col   

从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。

public class BufferedReaderJava {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        String sPath1 = "d:" + File.separator + "javatest" + File.separator + "CollectionTest1.java";
        BufferedReader r = new BufferedReader(new FileReader(new File(sPath1)));
        char[] ch = new char[1024];
        int result = 0;
        //确认流是否准备好
        if(r.ready()) {
            //读取数据
            while((result = r.read(ch)) != -1) {
                System.out.println(new String(ch, 0, result));
            }
        }
        
        r.close();
    }

}

使用readLine()方法每次读取一行

public class BufferedReaderJava {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        String sPath1 = "d:" + File.separator + "javatest" + File.separator + "CollectionTest1.java";
        BufferedReader r = new BufferedReader(new FileReader(new File(sPath1)));
        String str = null;
        while((str = r.readLine()) != null) {
            System.out.println(str);
        }
        
        r.close();
    }

}
public class BufferedReaderJava {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        String sPath1 = "d:" + File.separator + "javatest" + File.separator + "CollectionTest1.java";
        BufferedReader r = new BufferedReader(new FileReader(new File(sPath1)));
        //String str = null;
        int result = 0;
        //这里输出的时候每行首字符丢失,因为先read()了一个字符,然后输出的时候接下来readLine()的部分
        while((result = r.read()) != -1) {
            System.out.println(r.readLine());
        }
        
        r.close();
    }

}

IO_BufferedReader&BufferedWriter

标签:char   取数   buffered   tor   数据   部分   rjava   []   col   

原文地址:https://www.cnblogs.com/changzuidaerguai/p/9279905.html

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