🏠: Blog home page: Incoming bogey
📕: Today's article: [Java] IO series BufferedReader
💝: I hope my analysis of IO series can help you 🎈
🌱: Boji is still trying to learn JavaSE. If you have any questions or omissions, please give me more advice 🙏
☀️: On the way of self-study and growth, thank you for your company! No hurry , No Pause ! 💝
Premise:
⭐ I don't know if you have such doubts when I brush the questions at ordinary times. I'm familiar with BufferedReader, but I don't know the details and principles of use. I specially write this article to help you sort it out!
1. Basic introduction:
Buffered transliteration: the of the buffer;
BufferedReader: a buffered input stream, which inherits from the Reader;
BufferedReader:
- Read text from character input stream and buffer characters to provide efficient reading of characters, arrays and lines;
- You can specify the buffer size, or use the default size. The default value is large enough to be used for its purpose;
Generally, when we do basic Java algorithm problems, we create the basic syntax of BufferedReader:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Syntax parsing:
- System.in byte input stream
- new InputStreamReader converts a byte stream to a character stream
- new BufferedReader puts the character stream into the character stream buffer
2. BufferedReader function list
BufferedReader(Reader in) BufferedReader(Reader in, int size) void close() void mark(int markLimit) boolean markSupported() int read() int read(char[] buffer, int offset, int length) String readLine() boolean ready() void reset() long skip(long charCount)
Main usage:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str= br.readLine() ; char c = br.read() ;
After the BufferedReader object is created, we can use the read() method to read a character from the console or the readLine() method to read a string.
be careful:
-
The readLine() of the StringBuffer class starts from reading one line of content each time until the read is null. Then output.
-
The read() method of StringBuffer class reads the converted character of each number and then outputs it.
Reference here: Rookie tutorial
3. Case demonstration:
3.1 read multi character input from console
To read a character from the BufferedReader object, use the read() method. Its syntax is as follows:
int read( ) throws IOException
Each time the read() method is called, it reads a character from the input stream and returns it as an integer value. Returns - 1 when the flow ends. This method throws IOException.
The following program demonstrates using the read() method to continuously read characters from the console until the user enters q.
BRRead.java file code:
//Use BufferedReader to read characters on the console import java.io.*; public class BRRead { public static void main(String[] args) throws IOException { char c; // Use system In create BufferedReader BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Input character, Press 'q' Press the key to exit."); // Read character do { c = (char) br.read(); System.out.println(c); } while (c != 'q'); } } The compilation and running results of the above examples are as follows: Input character, Press 'q' Press the key to exit. runoob r u n o o b q q
3.2 read string from console
Reading a string from standard input requires the readLine() method of BufferedReader.
Its general format is:
String readLine( ) throws IOException
The following program reads and displays character lines until you enter the word "end".
BRReadLines.java Document code: //Use BufferedReader to read characters on the console import java.io.*; public class BRReadLines { public static void main(String[] args) throws IOException { // Use system In create BufferedReader BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str; System.out.println("Enter lines of text."); System.out.println("Enter 'end' to quit."); do { str = br.readLine(); System.out.println(str); } while (!str.equals("end")); } } The compilation and running results of the above examples are as follows: Enter lines of text. Enter 'end' to quit. This is line one This is line one This is line two This is line two end end
4. BufferedReader source code
The blogger wrote a great article. For source code analysis, please refer to this article:
https://www.cnblogs.com/skywang12345/p/io_23.html