实例如下:
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