Reference: http://blog.csdn.net/ilibaba/article/details/3955799
The general use principle of Java IO (many eye disorder, in fact, each class has a special role):
Here are the details:Introduction to Java IO input and output stream class (with image)
First, according to the data source (the whereabouts, that is,Granules) Categories: 1, Documents:
FileInputStream, FileOutputStream, (Byte stream)
FileReader, FileWriter (character stream)
2, byte array byte[]:
Bytearrayinputstream, Bytearrayoutputstream (Byte stream)
3, character array char[]:
CharArrayReader, Chararraywriter (character stream)
4. Strings string:
Stringbufferinputstream,stringbufferouputstream (Byte stream)
StringReader, StringWriter (character stream)
5. Network Data Flow:
InputStream, OutputStream, (Byte stream)
Reader, Writer (character stream)
Second, according to whether to format the output points:1. To format the output:
PrintStream (byte stream only), printwriter(Byte stream and character stream)
Three, according to whether to buffer points: (generally recommended use, can improve efficiency) 1, to buffer:
Bufferedinputstream, Bufferedoutputstream, (Byte stream)
BufferedReader, BufferedWriter (character stream)
Four, according to the data format: (the most fundamental, in addition to text files using character stream, all other byte stream priority) 1, binary format (as long as it is not determined to be plain text):
InputStream, OutputStream and all subclasses with Stream end
2, plain text format (including pure English and Chinese characters or other encoding method):
Reader, writer , and all subclasses of writer with reader
V. Special needs: 1. Conversion class from Stream to Reader,writer:
InputStreamReader, OutputStreamWriter
2. Object input and output:
ObjectInputStream, ObjectOutputStream
3. Inter-process communication:
Pipeinputstream, Pipeoutputstream, (Byte stream)
Pipereader, Pipewriter (character stream)
4. Merge input:
Sequenceinputstream
5, more special needs:
Pushbackinputstream, Pushbackreader, Linenumberinputstream, LineNumberReader
Steps to use the principle *************************************
The general guidelines for deciding which class to use and how to construct it are as follows (regardless of special needs):
First, consider what the most primitive data format is:
principle Four (binary or plain text, directly deciding whether to use a byte stream or a character stream )
Second, whether the stream needs to be converted:
principle Six 1th ( byte-to-character, character-to-byte )
Third, what is the source of the data (whereabouts):
principle One ( particle: basic unit, is file, byte array/character array, String,object)
Four, whether to buffer:
principle Three (Special note: It must be noted that ReadLine () has a definition , what is more than read, write a more special input or output method, usually more recommended to use, can improve efficiency
the normal inputstream/outputstream or reader/writer do not define the ReadLine () method , That is, you cannot output a string directly from the console ,
The buffered buffer stream has a defined readline (), but cannot be wrapped automatically, requires newline () manual generation , and PrintStream and PrintWriter have prin TLN () method )
Five, whether to format the output: (In fact, it is directly output)
principle Two (Printstream,printwriter)
A few examples **********************************
Java simple code for copying files: 1. Use byte stream (all can come out)
1 Packageiotest;2 3 ImportJava.io.*;4 ImportJava.util.*;5 /**6 * A simple example of Java copying Files7 * */8 Public classCopywithoutbuffer {9 Public Static voidMain (string[] args) {Ten LongStart =System.currenttimemillis (); OneSYSTEM.OUT.PRINTLN ("Begin copy ....")); A Try { -FileInputStream FIS =NewFileInputStream ("E:\\12.mp4"); -FileOutputStream fos =NewFileOutputStream ("E:\\123.mp4"); the //buffered streams are used here to improve efficiency -Bufferedinputstream bis =NewBufferedinputstream (FIS); -Bufferedoutputstream BOS =NewBufferedoutputstream (FOS); - Try { + byte[] buf =New byte[1024]; - ////using buffer + intRead =Bis.read (BUF); A while(Read!=-1){ atBos.write (Buf,0,read);//because BUF may be dissatisfied at the end of the reading, read will prevail. -Read =Bis.read (BUF); - } - - // //do not use a buff, do not use a byte array, much less efficient, time-consuming - //int read = Bis.read ();//read one byte at a time in //While (read!=-1) { - //Bos.write (read); to //read = Bis.read (); + // } - bos.close (); the fos.close (); * bis.close (); $ fis.close ();Panax Notoginseng}Catch(IOException e) { - e.printstacktrace (); the } + A}Catch(FileNotFoundException e) { the e.printstacktrace (); + } - $ LongEnd =System.currenttimemillis (); $System.out.println ("Copy complete,use Times:" + (End-start) + "MS"); - } - the}
View Code2. Use a character stream (can only handle plain text files (faster than byte stream processing), video audio will be problematic):
1 Packageiotest;2 3 ImportJava.io.*;4 5 Public classIoreadertest {6 Public Static voidMain (string[] args) {7System.out.println ("Start");8 LongStart =System.currenttimemillis ();9 Try {TenFileReader FR =NewFileReader ("E:\\123.txt"); OneFileWriter FW =NewFileWriter ("E:\\123456.txt"); A /* - bufferedreader br = new BufferedReader (FR); - String ABC = br.readline ();//bufferedreader defines the ReadLine () method the //If you want to wrap, you need to generate the Br.newline () manually. - SYSTEM.OUT.PRINTLN (ABC); - */ - intRead =Fr.read (); + while(Read!=-1){ - Fw.write (read); +Read =Fr.read (); A } at fw.close (); - fr.close (); - LongEnd =System.currenttimemillis (); -SYSTEM.OUT.PRINTLN ("Time Consuming:" + (End-start) + "MS"); -}Catch(FileNotFoundException e) { - e.printstacktrace (); in}Catch(IOException e) { - e.printstacktrace (); to } + - } the *}
View Code
Usage principles for various classes of IO streams in Java