1. printstream class
We all know that the int type occupies 4 bytes in Java. For example, 1 indicates 00000000 00000000 00000000 00000001 in memory. If we directly use the outputstream object to output data to a file, the program will directly write 00000001 to the file. Needless to say, opening it in notepad is certainly a strange character. Now we can use the printstream class to convert the data in the memory to the bytes type and then output it to the file.
The printstream class automatically converts data to characters, so that when we open it in a text editor, the integer variable 1 will see a 1 instead of garbled characters. For example:
Package CLS; import Java. io. *; public class printstreamdemo {public static void main (string [] ARGs) throws exception {// use the outputstream object to output fileoutputstream Fos = new fileoutputstream ("outputstream.txt"); FOS. write (1); // write 1fos into integer data. close (); // use the printstream object to output printstream PS = new printstream (New fileoutputstream ("printstream.txt"); PS. println (2); // write the integer ps. println (3.14); // write the double ps. println (New strin G ("Hello world! "); // Write string type ps. Close ();}}
2. Reader and writer
Java. Io. Reader, java. Io. writer, and their child classes are related to processing the producer stream. That is to say, these classes process output data in units of a single character (2 bytes) and are suitable for reading and writing plain text files.
Similar to Java. Io. inputstream and Java. Io. outputstream, reader and writer are abstract classes. We generally use their subclasses to perform related operations.
The following code describes how to use inputstreamreader and outputstreamwriter:
Package CLS; import Java. io. *; public class iodemo {public static void main (string [] ARGs) throws exception {fileinputstream FD = new fileinputstream ("input.txt "); // Add the character processing function inputstreamreader ISR = new inputstreamreader (fiis) to the input file stream; // read the int CH = 0 from the file; while (CH = ISR. read ())! =-1) {system. Out. Print (char) CH);} system. Out. println (); // close stream ISR. Close ();}}
Filereader and filewriter can read and write plain text files conveniently. For example, convert the line breaks of text files in Linux to line breaks in Windows format:
Package CLS; import Java. io. *; Class filereaderdemo {public static void main (string [] ARGs) throws exception {filereader = new filereader ("input.txt"); filewriter = new filewriter ("output.txt "); int CH = 0; char [] wrapchar = new char [] {'\ R',' \ n'}; while (CH = filereader. read ())! =-1) {If (CH = '\ n') // if it is a linefeed {// write \ r \ n linefeed filewriter. write (wrapchar);} else {filewriter. write (CH) ;}// close the stream filereader. close (); filewriter. close ();}}
Bufferedreader and bufferedwriter each have a buffer zone of 8192 characters. When bufferedreader reads text files, it will try to fill the buffer zone. If the read () method is called, the class will read data from the buffer zone. Similarly, bufferedwriter writes data to the buffer before writing data to the file after the buffer is full.
Example program:
Package CLS; import Java. io. *; Class bufferedreaderdemo {public static void main (string [] ARGs) throws ioexception {// character input stream inputstreamreader inputreader = new inputstreamreader (system. in); bufferedreader bufreader = new bufferedreader (inputreader); // adds the buffer function to the input // file character output stream filewriter = new filewriter ("output.txt "); bufferedwriter bufwriter = new bufferedwriter (filewriter); // adds the buffer function to the output stream. String in Put = NULL; // each time a row of characters is read from the standard input until quit ends while (! (Input = bufreader. readline ()). equals ("quit") {bufwriter. write (input); bufwriter. newline (); // outputs OS-related line breaks} // closes the stream bufreader. close (); bufwriter. close ();}}