java中讲讲BufferedReader的用法,举例?

本文通过两个示例详细介绍了Java中BufferedReader类的基本使用方法。包括如何从文件读取字符串并输出到控制台,以及如何从StringReader读取内容进行处理。适合初学者学习Java IO操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2.3 BufferedReader的用法 
马克-to-win:BufferedReader和BufferedInputStream差不多,只不过一个处理字符,一个处理字节。buffer(缓存)什么的原理都是一样的。

例:2.3.
import java.io.*;
public class TestMark_to_win {
    public static void main(String args[]) throws Exception {
        /* from file to string then to "multiline string" then to console. */
        String s2, s3 = new String();
        /*Creates a new FileReader, given the name of the file to read from.BufferedReader(Reader in) Create a buffering character-input stream
that uses a default-sized input buffer.*/
        BufferedReader in = new BufferedReader(new FileReader("c:/4.txt"));
        /*readLine is a method from BufferedReader: Read a line of text.
Returns: A String containing the contents of the line, not including
any line-termination characters, or null if the end of the stream has
been reached */
        while ((s2 = in.readLine()) != null) {
            s3 += s2 + "\n";
        }
        System.out.println(s3);
        in.close();
    }
}

 

例:2.3.2

import java.io.*;
public class TestMark_to_win {
    public static void main(String args[]) throws Exception {
        // /////////////////////// from StringReader to file:
        String s = "你们" + "\n" + "他们我们" + "\n";
        String s1 = "";
        StringReader in2 = new StringReader(s);
        in2.reset();
        BufferedReader in4 = new BufferedReader(in2);
        while ((s1 = in4.readLine()) != null) {
            System.out.println("output" + s1);
        }
    }
}

更多请见:https://blog.youkuaiyun.com/qq_44639795/article/details/102611549

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mark_to_win

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值