用Java读取数据

发布于 2024-12-11 06:14:47 字数 79 浏览 0 评论 0原文

使用 BufferedReaderBufferedInputStream 有什么区别?

What's the difference between using a BufferedReader and a BufferedInputStream?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

抹茶夏天i‖ 2024-12-18 06:14:47

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

继承自 Reader< 的任何类/a> 或 Writer< /a> 处理 16 位 unicode 字符数据,而从 InputStreamOutputStream 涉及处理二进制数据。类 InputStreamReaderOutputStreamWriter 可用于桥接两类数据。

A BufferedReader is used for reading character data. A BufferedOutputStream is used for writing binary data.

Any classes inheriting from Reader or Writer deal with 16-bit unicode character data, whereas classes inherting from InputStream or OutputStream are concerned with processing binary data. The classes InputStreamReader and OutputStreamWriter can be used to bridge between the two classes of data.

汹涌人海 2024-12-18 06:14:47

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);
        }

Bufferedreader reads data from a file as a string. BufferedOutputStream writes to a file in bytes. BufferedInputStream reads data in bytes
Sample to Bufferedreader:

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

Sample to 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 reads in byte:
Sample
:

 //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);
        }
沉默的熊 2024-12-18 06:14:47

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

As the names imply, one is for reading data, and the other is for outputting data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文