Read the file:
BufferedReader
Reads text from the character input stream, buffering individual characters, providing 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.
You can localize a program that uses DataInputStream to input by source, by replacing each datainputstream with the appropriate bufferedreader.
To specify how the file is encoded, enter the following modifications:
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, buffering individual characters, providing efficient writes of individual characters, arrays, and strings.
You can specify the size of the buffer, or accept the default size. In most cases, the default value is large enough.
The class provides the NewLine () method, which uses the platform's own concept of row delimiters, which is defined by the system Properties Line.separator. Not all platforms use New line characters (' \ n ') to terminate each row. Therefore calling this method to terminate each output line is preferable to writing a new line character directly.
Normally Writer sends its output immediately to the underlying character or byte stream. Unless prompted for output, it is recommended to use BufferedWriter to wrap any Writer whose write () operation might be expensive, such as Filewriters and Outputstreamwriters. For example
PrintWriter out
= new PrintWriter (new BufferedWriter (New FileWriter ("Foo.out"));
The buffer printwriter the output of the file. Without buffering, each call to the print () method causes the character to be converted to bytes and then immediately written to the file, which is extremely inefficient.
In order to specify how the file is encoded:
PrintWriter out = new PrintWriter (new BufferedWriter (New OutputStreamWriter (FileOutputStream), " GB2312 ")));
Java file read-write operation specifies the encoding format