用Java读取数据

时间:2011-10-23 21:02:59

标签: java io bufferedreader

使用BufferedReaderBufferedInputStream之间的区别是什么?

3 个答案:

答案 0 :(得分:4)

BufferedReader用于读取字符数据。 BufferedOutputStream用于编写二进制数据。

任何继承自ReaderWriter的类都处理16位unicode字符数据,而来自InputStreamOutputStream的类涉及处理二进制数据。类InputStreamReaderOutputStreamWriter可用于桥接两类数据。

答案 1 :(得分:3)

Bufferedreader从文件中读取数据作为字符串。 BufferedOutputStream以字节为单位写入文件。 BufferedInputStream以字节为单位读取数据 样品到Bufferedreader:

try {
       BufferedReader br = new BufferedReader(new FileReader(new File(your_file));
       while ((thisLine = br.readLine()) != null) { 
         System.out.println(thisLine);
       }
     } 

BufferedOutputStream示例:

//Construct the BufferedOutputStream object
        bufferedOutput = new BufferedOutputStream(new FileOutputStream(filename));

        //Start writing to the output stream
        bufferedOutput.write("Line 1".getBytes());
        bufferedOutput.write("\r\n".getBytes());
        bufferedOutput.write("Line 2".getBytes());
        bufferedOutput.write("\r\n".getBytes());

Bufferedinputstream读取字节:
样品 :

 //Construct the BufferedInputStream object
        bufferedInput = new BufferedInputStream(new FileInputStream(filename));

        int bytesRead = 0;


        while ((bytesRead = bufferedInput.read(buffer)) != -1) {                

            String chunk = new String(buffer, 0, bytesRead);
            System.out.print(chunk);
        }

答案 2 :(得分:1)

顾名思义,一个用于读取数据,另一个用于输出数据。