Java readLine()、read()、read(char cbuf[])、read(char cbuf[], int off, int len)详解

BufferReader inherited Reader, mainly readLine (), read (), read (char cbuf []), read (char cbuf [], int off, int len) method, here I come and explain the use of these methods Notes .

First create a test.txt file (Windows systems) in the D drive, the contents of the file:

Hello
Android
2020-01-15

readLine()

This method is used to read a line of character data stream, is used as follows:

package s1;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class X {
	public static void main(String[] args) {
		File file = new File("D:\\test.txt");
		FileReader fileReader;
		BufferedReader bufferedReader = null;
		try {
			fileReader = new FileReader(file);
			bufferedReader = new BufferedReader(fileReader);
			StringBuilder stringBuilder = new StringBuilder();
			String s;
			while ((s = bufferedReader.readLine()) != null) {
				stringBuilder.append(s);
				stringBuilder.append("\n");
			}
			System.out.println(stringBuilder.toString());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(bufferedReader != null) {
				try {
					bufferedReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

Results are as follows:

Hello
Android
2020-01-15

read()

This method each read only one character, usage and readLine () similar, do not write more.

read(char cbuf[])

This method is a time to read cbuf.length characters to cbuf inside the array, the source code inside the method call is actually read (char cbuf [], int off, int len). That is read (cbuf, 0, cbuf.length)

read(char cbuf[], int off, int len)

This method is the correct meaning: Reads len characters, then starting with an cbuf [off], attention must not be greater than len + off cbuf.length, otherwise it will report an array bounds exception. The common misconception is: BufferReader skipped off characters, then read len characters to cubf array.

package s1;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;

public class X {
	public static void main(String[] args) {
		File file = new File("D:\\test.txt");
		FileReader fileReader;
		BufferedReader bufferedReader = null;
		try {
			fileReader = new FileReader(file);
			bufferedReader = new BufferedReader(fileReader);
			char[] cbuf = new char[4];
			bufferedReader.read(cbuf, 2, cbuf.length - 2);
			System.out.println(Arrays.toString(cbuf));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(bufferedReader != null) {
				try {
					bufferedReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

operation result:

[ , , H, e]

 

 

Published 74 original articles · won praise 4 · views 10000 +

Guess you like

Origin blog.csdn.net/GracefulGuigui/article/details/103993968