1. Basic structure of IO class library
The IO interfaces based on byte operations are InputStream and Outputstream,inputstream, respectively, as shown in the class structure diagram below:
Similar to InputStream, the OutputStream class also has the same class structure diagram.
The use of each subclass can refer to the API documentation for the JDK, and here's what we need to note: The way data is manipulated can be combined as follows:
New InputStreamReader (new FileInputStream ("h:\\haha.txt""utf-8 ");
From the above code can be seen, InputStreamReader can read from the FileInputStream data, from the source can be seen, in fact, not only inputstreamreader, all the IO class can be combined in this way to use.
It is also important to note that the flow will have to be written to the final location, disk or network, from the OutputStream class diagram can be seen in the write network is actually a write file operation, but the bottom is transmitted over the network.
2. Character-based IO operation interface
Whether it is disk or network data transmission, is in bytes, but the general common data operations in the program are in characters (char in Java occupies 2 bytes, C + + char occupies 1 bytes), which requires that we have an IO interface to manipulate characters, To deal with the encoding conversion problem of character and Byte, that is, the write and reader interfaces and their implementation classes, and their class interface diagrams are as follows:
The most important way to read a character interface reader is read (), which reads the characters and returns the number of characters read, either writer or reader, which defines only the way in which the data characters are read or written, that is, how to write or read, but there is no provision for the data to be written to ( such as disk or network).
3, byte-to-character conversion interface
Sometimes data persistence and network transmission are performed in bytes, and all require the conversion of bytes and characters to each other.
/*** Use FileReader to read files*/@Test Public voidTestfilereader ()throwsIOException {filereader FileReader=NewFileReader ("H:\\haha.txt"); Char[] Buff =New Char[512]; StringBuffer StringBuffer=NewStringBuffer (); while(Filereader.read (Buff) > 0) {stringbuffer.append (buff); } filereader.close (); System.out.print (Stringbuffer.tostring ());}/*** Use FileReader to read the file and then FileWriter write to another file*/@Test Public voidTestfilereaderandfilewriter ()throwsIOException {filereader FileReader=NewFileReader ("H:\\haha.txt"); Char[] Buff =New Char[512]; StringBuffer StringBuffer=NewStringBuffer (); while(Filereader.read (Buff) > 0) {stringbuffer.append (buff); } System.out.println (Stringbuffer.tostring ()); FileWriter FileWriter=NewFileWriter ("H:\\haha2.txt"); Filewriter.write (stringbuffer.tostring (). Trim ()); Filewriter.close (); System.out.println ("Write file succeeded");}/*** Use InputStreamReader to read files*/@Test Public voidTestinputstreamreader ()throwsIOException {inputstreamreader InputStreamReader=NewInputStreamReader (NewFileInputStream ("H:\\haha.txt"), "Utf-8"); Char[] Buff =New Char[512]; StringBuffer StringBuffer=NewStringBuffer (); while(Inputstreamreader.read (Buff) > 0) {stringbuffer.append (buff); } System.out.println (Stringbuffer.tostring ());} @Test Public voidTESTINTPUTSTREAM2 ()throwsIOException {inputstreamreader InputStreamReader=NewInputStreamReader (NewStringBufferInputStream ("Hello World")); Char[] Buff =New Char[512]; intn =inputstreamreader.read (Buff); SYSTEM.OUT.PRINTLN (n); SYSTEM.OUT.PRINTLN (buff);}/*** Use InputStreamReader to read the file and then OutputStreamWriter write to another file*/@Test Public voidTestoutputstreamwriter ()throwsIOException {inputstreamreader InputStreamReader=NewInputStreamReader (NewFileInputStream ("H:\\haha.txt"), "Utf-8"); Char[] Buff =New Char[512]; StringBuffer StringBuffer=NewStringBuffer (); while(Inputstreamreader.read (Buff) > 0) {stringbuffer.append (buff); } System.out.println (Stringbuffer.tostring ()); OutputStreamWriter OutputStreamWriter=NewOutputStreamWriter (NewFileOutputStream ("H:\\haha2.txt"), "Utf-8"); Outputstreamwriter.write (stringbuffer.tostring (). Trim ()); Outputstreamwriter.close ();}
Note: The FileReader class inherits the Inputstreamreader,filereader read file stream, which is decoded by Streamdecoder to Char, and its decoded character set uses the default character set. In Java, we should use the file object to determine if a file exists, and if we open it with FileOutputStream or filewriter, it will definitely be overwritten.
Document material taken from: https://www.ibm.com/developerworks/cn/java/j-lo-javaio/
Java IO Interface---Byte manipulation character operations disk Operation network operation