Reader:
Sub-class for reader:
1.BufferedReader :
FileReader does not provide the ability to read lines of text, BufferedReader can specify the buffer size, wrapping the Read method to efficiently read the character stream. Provides efficient reading of characters, arrays, and rows.
开发文档中的介绍:
Reads text from the character input stream, buffering individual characters, enabling efficient reading of characters, arrays, and rows. You can specify the size of the buffer, or you can use the default size. In most cases, the default value is large enough.
Typically, every read request made by reader results in a corresponding read request to the underlying character or byte stream.
Therefore, it is recommended to use BufferedReader to wrap all of their read () operations with potentially expensive Reader (such as FileReader and InputStreamReader).
For example
BufferedReader in = new BufferedReader (New FileReader ("foo.in"));
The input for the specified file is buffered. Without buffering, each call to read () or readLine () causes the bytes to be read from the file and returned after it is converted to characters, which is extremely inefficient.
By replacing each datainputstream with the appropriate bufferedreader, you can localize a program that uses DataInputStream for text input.
Construction Method Summary |
BufferedReader (Reader in) Creates a buffered character input stream that uses the default size input buffer. |
BufferedReader (Reader in, int sz) Creates a buffered character input stream that uses the specified size input buffer. |
Use:
I just want to say filereader don't use it directly, put it in BufferedReader. This saves time for reading large text files. Because there are buffered text files, if not buffered, the text file will be accessed again and again through the Read method.
2.LineNumberReader:
As with Bufferedreade, there is a unique approach, setLineNumber(int)
and getLineNumber()
.
public class LineNumberReader extends BufferedReader
开发文档中的介绍:
The buffer character input stream that tracks the line number. This class defines methods setLineNumber(int)
and getLineNumber()
they can be used to set and get the current line number, respectively.
By default, the line numbering starts at 0. The line number increments at each line terminator with the data read, and the row number can be called by setLineNumber(int)
a call.
Note, however, that the setLineNumber(int)
current position in the stream is not actually changed; it only changes the value that will be returned by Getlinenumber () .
You can think of the line ending at one o'clock when the following symbol is encountered: newline (' \ n '), carriage return (' \ R '), carriage return followed by line break.
Construction Method Summary |
LineNumberReader (Reader in) Creates a new line number reader with the size of the default input buffer. |
LineNumberReader (Reader in, int sz) Creates a new line number, reader, that reads the characters into a buffer of the given size. |
3. InputStreamReader
The byte flows to a character stream of bridges, which are generally combined with bufferedreader.
You can specify the encoding in the constructor, if not specified by the underlying operating system will be the default encoding, may appear on different platforms garbled phenomenon!
You must use the InputStreamReader class when you want to specify the encoding method. Instead of FileReader with subclasses.
开发文档中的介绍:
InputStreamReader is a bridge of byte flow to a character stream: It uses the specified charset
read byte and decodes it to a character. The character set it uses can be specified or explicitly given by name, or it can accept the default character set of the platform.
Each call to a read () method in InputStreamReader causes one or more bytes to be read from the underlying input stream. To enable a valid conversion from byte to character, you can read more bytes in advance from the underlying stream to exceed the bytes required to satisfy the current read operation.
In order to achieve maximum efficiency, it is necessary to consider packaging InputStreamReader within the BufferedReader. For example:
BufferedReader in = new BufferedReader (new InputStreamReader (system.in));
Construction Method Summary |
InputStreamReader (InputStream in) Create a inputstreamreader that uses the default character set. |
InputStreamReader (InputStream in, Charset CS) Creates a inputstreamreader using the given character set. |
InputStreamReader (InputStream in, Charsetdecoder Dec) Creates a inputstreamreader using the given character set decoder. |
InputStreamReader (InputStream in, String CharsetName) Creates a inputstreamreader that uses the specified character set. |
4. FileReader
public class FileReader extends InputStreamReader
This is the same as InputStreamReader. The difference lies in the constructor.
You must use the InputStreamReader class when you want to specify the encoding method.
开发文档中的介绍:
A handy class for reading character files. The construction method of this class assumes that both the default character encoding and the default byte buffer size are appropriate.
To specify these values yourself, you can first construct a inputstreamreader on the FileInputStream.
FileReader
Used to read a character stream. To read the raw byte stream, consider using the FileInputStream
.
Construction Method Summary |
FileReader (File file) Creates a new filereader given the File from which the data is read. |
FileReader (FileDescriptor FD) Creates a new filereader given the filedescriptor from which the data is read. |
FileReader (String fileName) Creates a new FileReader given the file name from which the data is read. |
Writer
Writer's subclass:
It is obvious that writer is corresponding to reader, reader is used for character readout operation, writer is used for character write operation.
It means a printwriter:
Printstrean is written to a string of 8bit data.
PrintWriter is written to a string of 16bit data.
PrintStream is used to manipulate byte,
PrintWriter is used to manipulate Unicode,
PrintStream is a subclass of OutputStream, and PrintWriter is a subclass of writer.
The System.out variable we commonly use is the PrintStream instance. The corresponding character stream class is the PrintWriter class.
开发文档中的介绍:
Prints the formatted representation of the object to the text output stream. This class implements PrintStream
all the print methods in. It does not contain methods for writing raw bytes, and for these bytes, the program should write using an encoded byte stream.
PrintStream
Unlike classes, if automatic refresh is enabled, this can only be done when one of the methods of println,printf , or format is called, rather than when a newline character is output. These methods use the platform-owned line delimiter concept, rather than line breaks.
Methods in this class do not throw I/O exceptions, although some of their construction methods may throw exceptions. The client may query whether the call has an checkError()
error.
Construction Method Summary |
PrintWriter (File file) Creates a new printwriter that does not have an automatic row refresh using the specified file. |
PrintWriter (File file, String CSN) Creates a new printwriter with the specified file and character set and without the auto-brush line. |
PrintWriter (OutputStream out) Creates a new printwriter with no automatic row refresh based on the existing outputstream. |
PrintWriter (OutputStream out, Boolean AutoFlush) Create a new printwriter from the existing outputstream. |
PrintWriter (String fileName) Creates a new printwriter with the specified file name without automatic row refresh. |
PrintWriter (String fileName, String CSN) Creates a new printwriter with the specified file name and character set without automatic row refresh. |
PrintWriter (Writer out) Creates a new printwriter with no automatic row refresh. |
PrintWriter (Writer out, Boolean AutoFlush) Create a new printwriter. |
The difference between stream,reader/writer,buffered (2)