首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >java中讲讲BufferedReader的用法,举例?

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

作者头像
马克java社区
修改2021-05-10 10:50:06
修改2021-05-10 10:50:06
8590
举报
文章被收录于专栏:java大数据java大数据

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.csdn.net/qq_44639795/article/details/102611549

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档