How:What:Java 中实现readLine()(逐行读取数据)的一个实例

本文介绍了一个使用Java进行文件读取的实例,详细解释了涉及的关键类如FileInputStream、BufferedReader及InputStreamReader的作用与使用方法,并说明了readLine()方法如何逐行读取文件。

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

实例如下:

FileInputStream f=new FileInputStream("myEncodeblocks.txt");
                BufferedReader tmp_reader=new BufferedReader(new InputStreamReader(f));
                String tmp_string=tmp_reader.readLine();

实现该实例几个要素:

    1.文件(本例中是myEncodeblocks.txt,存储在当前目录,即可执行文件class所在目录)

    2.BufferedReader实例,构造函数依赖InputStreamReader实例

    3.InputStreamReader实例

        An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset.

        该例中用的构造函数:InputStreamReader(InputStream in)
          Create an InputStreamReader that uses the default charset.

    4.FileInputStream实例

        A FileInputStream obtains input bytes from a file in a file system. What files are available depends on the host environment.

        本实例使用的构造函数:

        FileInputStream(String name)
          Creates a FileInputStream by opening a connection to an actual file, the file named by the path name name in the file system.

        一般我们从键盘输入字符流,而FileInputStream从文件系统中的文件中获取字符流

    5.readLine()

           该方法属于BufferedReader,每次会自动从下行开始读取,遇到“\n”或“\r”则改行结束

Read a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

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

    


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值