关闭

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

标签: javafilestringcharacterstreaminput
6265人阅读 评论(0) 收藏 举报
分类:

实例如下:

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

    


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:197717次
    • 积分:2315
    • 等级:
    • 排名:第16109名
    • 原创:66篇
    • 转载:27篇
    • 译文:0篇
    • 评论:14条
    最新评论