This article is not intended to introduce the Java input and output stream mechanism, only for personal notes
As a Java side dish, every time the Internet search other people's Java read and write files of the program reference, always feel confused, why to declare so many classes, rules is what, completely divided からない, so with a little bit of doubt about it;
There are two classes of input and output modes in Java, byte-oriented (Inputstream&outputstream) and character-oriented (Reader&writer), and a class (file) for processing files and folders.
Java input and output stream class using decorator mode, from the above 5 classes derived from a lot of other classes, here to put a picture on the internet, we feel the next
Java supports many types of input sources and outputs, but it's not going to unfold, I just want to talk about the read and output of the file. It is well known that computers accessing external devices are time consuming and access buffers are fast. Therefore, in order to improve the efficiency of reading and writing files, the buffer mechanism needs to be added in addition to the necessary reading and writing mechanisms between program and flow nodes.
Specific examples are as follows:
Packagecom.practise.test;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;ImportJava.io.InputStreamReader;ImportJava.io.BufferedReader;ImportJava.io.OutputStreamWriter;ImportJava.io.BufferedWriter;//All classes ending with Stream are byte-oriented, such as Fileinputstream/fileoutputstream//classes ending in Reader/writer are character-oriented//classes beginning with File are oriented to external memory//classes that start with Buffered are buffer-oriented Public classReadAndWrite { Public Static voidMain (string[] args) {String path= "D:\\coding\\java\\testjava\\testdata\\"; Try{File InFile=NewFile (path + "In.txt")); File OutFile=NewFile (path + "OUT.txt")); InputStreamReader INSR=NewInputStreamReader (NewFileInputStream (inFile)); BufferedReader INBR=NewBufferedReader (INSR); OutputStreamWriter OUTSW=NewOutputStreamWriter (NewFileOutputStream (outFile)); BufferedWriter OUTBW=NewBufferedWriter (OUTSW); String Line= ""; while(line = Inbr.readline ())! =NULL) {Outbw.write ( line+ "\ r \ n"); } outbw.close (); Outsw.close (); Inbr.close (); Insr.close (); } Catch(Exception e) {e.printstacktrace (); } }}
View Code
Java input and output stream