A character input stream
java.io.Reader abstract class is a superclass of all character streams for reading, the character information can be read into memory.
It defines the basic functionality common method of character input stream.
public void close (): Closes this stream and releases any system resources associated with this stream.
public int read (): read a character from the input stream.
public int read (char [] cbuf ): number of characters read from the input stream, and stores them in the character array cbuf.
Reader abstract subclass structure :
Note : Brunette node stream, to handle the flow of light.
Two, FileReader class
java.io.FileReader class is a subclass of reading character files. The system default character encoding and the default character buffer during construction.
java.io.FileReader extends InputStreamReader extends Reader inheritance.
Tips:
① character encoding: the mapping rules byte characters, windows in Chinese encoding system default GBK coding table, or a UTF-8.
② character buffer: a byte array, for temporarily storing data bytes.
1, construction method
FileReader (File file): Creates a new FileReader, given the File object to be read.
FileReader (String fileName): Creates a new FileReader, name of the file to be read given.
Parameters : the data source to read the file.
Path to the file: String fileName
File file: file object
The role of the constructor :
① create a FileReader objects
② will FileReader object to a file to be read
The character input stream using the steps :
① create a FileReader object constructor to bind the data source to read.
② use FileReader object to read the file read.
③ release resources.
2, read character data
(1) the characters are read: Read method, data can be read each time a character is to enhance the int type, to the end of the file is read, returns - 1 , read cycle, the code demonstrates the use of:
. 1 public static void main (String [] args) throws IOException {
2 // 1. Create FileReader object constructor bound to read the data source
. 3 FileReader fr = new new FileReader ( "E: \\ C.txt" );
4 // 2. use FileReader object read to read the file
. 5 // int read () read a single character and returns.
. 6 int len = 0 ;
. 7 the while ((len = fr.read ()) = -. 1! ) {
. 8 of System.out.print (( char ) len);
. 9 }
10
. 11 // 3. releasing resources
12 fr.close();
13 }
(2) using the character array to read : Read (char [] CBUF) , each read b the length of the character array, returns the number of valid characters to read, read to the end, it returns - . 1 , use Code demo:
1 public static void main(String[] args) throws IOException {
2 //1.创建FileReader对象,构造方法中绑定要读取的数据源
3 FileReader fr = new FileReader("E:\\c.txt");
4 //2.使用FileReader对象中的方法read读取文件
5
6 //int read(char[] cbuf)一次读取多个字符,将字符读入数组。
7 char[] cs = new char[1024];//存储读取到的多个字符
8 int len = 0;//记录的是每次读取的有效字符个数
9 while((len = fr.read(cs))!=-1){
10 /*
11 String类的构造方法
12 String(char[] value) 把字符数组转换为字符串
13 String(char[] value, int offset, int count) 把字符数组的一部分转换为字符串 offset数组的开始索引 count转换的个数
14 */
15 System.out.println(new String(cs,0,len));
16 }
17
18 //3.释放资源
19 fr.close();
20 }