Read files:
Bufferedreader
Read text from the character input stream, buffer each character, so as to provide efficient reading of characters, arrays and rows.
You can specify the buffer size or use the default size. In most cases, the default value is large enough.
Generally, each read request made by reader leads to a corresponding Read Request for the basic character or byte stream. Therefore, we recommend that you use bufferedreader to wrap all the reader (such as filereader and inputstreamreader) whose read () operations may be costly ). For example,
Bufferedreader in
= New bufferedreader (New filereader ("foo. In "));
The input of the specified file is buffered. If there is no buffer, each call to read () or Readline () will result in reading bytes from the file and converting it to a character and returning it, which is extremely inefficient.
You can localize the program that uses datainputstream to input the original text by replacing each datainputstream with a suitable bufferedreader.
To specify the file encoding method, go to the following modification:
// Bufferedreader in = new bufferedreader (New filereader (savefilename ));
Bufferedreader in = new bufferedreader (New inputstreamreader (New fileinputstream (savefilename), "gb2312 "));
Write File:
Bufferedwriter
Writes text to the character output stream to buffer each character, providing efficient writing of a single character, array, and string.
You can specify the buffer size or accept the default size. In most cases, the default value is large enough.
This class provides the newline () method, which uses the platform's own line separator concept, which is defined by the system attribute line. separator. Not all platforms use the newline ('\ n') to terminate each row. Therefore, calling this method to terminate each output line is better than writing a new line character directly.
Usually, writer sends the output to the base character or byte stream immediately. Unless output is prompted, it is recommended that bufferedwriter be used to wrap all writer (such as filewriters and outputstreamwriters) with high overhead write () operations ). For example,
Printwriter out
= New printwriter (New bufferedwriter (New filewriter ("foo. Out ")));
Buffer the output of printwriter to the file. If there is no buffer, each call to the print () method will lead to the conversion of characters into bytes, and then immediately write to the file, which is extremely inefficient.
To specify the file encoding method:
Printwriter out = new printwriter (New bufferedwriter (New outputstreamwriter (New fileoutputstream (savefilename), "gb2312 ")));