The most important 1,io system in the entire Java.io package is 5 classes and one interface. 5 classes refer to aFile, OutputStream, InputStream, Writer, Reader; An interface refers to aSerializable. Mastering the core operations of these IO then there is a preliminary understanding of the IO architecture in Java. java.io package defines a number of stream type classes to implement input and output functions, from different angles can be divided into: ~ According to the data flow direction of the different can be divided intoinput StreamAndoutput Stream。 ~ According to the different data processing units can be divided intoByte streamAndcharacter Stream。 The following graphs can visually describe most of the classes and relationships in the IO architecture. You can tell by the class name that this class is basically an operation or something.
, 2, a simple copy of the file instance
File F1 =NewFile (Args[0]);//file object for source filesFile F2 =NewFile (Args[1]);//the file object for the target filesInputStream input =NULL;//prepare the input stream object and read the source file.OutputStream out =NULL;//prepare the output stream object and write to the target file.input =NewFileInputStream (F1); out=NewFileOutputStream (F2); inttemp = 0; while(temp = Input.read ())! =-1) {//Start CopyOut.write (temp);//Write while reading} input.close (); //CloseOut.close ();//Close
3, Other related class system class to IO support System.out is actually a print stream printstream, the output object is the console, System.err is also PrintStream, two actually no difference, Just logically err to handle the wrong information only system.in is actually a inputstream, the input source is a keyboard and other devices, in the program directly used as InputStream to obtain data, while waiting to obtain data, the thread blocking wait. The System class also provides a way to SetOut Setin, which allows the user to define the source and destination of the data themselves. But in general, it is not to modify this thing.
InputStream input = system.in; // receiving data from the keyboard byte New byte [5]; // open up space to receive data System.out.print ("Please enter content:"); // Prompt Information int len = input.read (b); // Receive Data New String (b,0, Len)); Input.close (); // close the input stream
The Bufferdreader class, which is used to read from the buffer, puts the byte data read from the InputStream into the buffer and reads it out. His constructor only accepts instances of the character input stream, So we need to use InputStreamReader to convert InputStream into reader, which in fact embodies a design pattern used in the Javaio system, the decorative mode (without having to change the original class file and use inheritance, The ability to dynamically extend an object. It is by creating a wrapper object, that is, decorating to wrap the real object. )
BufferedReader buf =NULL;//declaring ObjectsBUF =NewBufferedReader (NewInputStreamReader (system.in));//Convert a stream of bytes into a character streamString str =NULL;//Receive input contentSystem.out.print ("Please enter content:") ; Try{str= Buf.readline ();//reading a row of data}Catch(IOException e) {e.printstacktrace (); //Output Information} System.out.println ("The input is:" + str);
The scanner class, which is a specialized, easy-to-use input data class provided by Java, can also be easily validated for input data, and his construction method supports a file parameter or a InputStream parameter, when used, The corresponding data in the stream can be obtained directly using the next series method.
New // Specify the action file NULL ; New // receiving data from the keyboard New StringBuffer (); while (Scan.hasnext ()) { str.append (Scan.next ()). Append (// fetch data System.out.println ("File contents are:" + str); }
Sequenceinputstream merge stream, mainly about two file contents merged one.
InputStream is1 =NULL;//input stream 1InputStream Is2 =NULL;//input stream 1OutputStream OS =NULL;//output StreamSequenceinputstream sis =NULL;//Merging flowsIs1 =NewFileInputStream ("D:" + File.separator + "A.txt") ; Is2=NewFileInputStream ("D:" + File.separator + "B.txt") ; OS=NewFileOutputStream ("D:" + File.separator + "Ab.txt") ; SIS=NewSequenceinputstream (IS1,IS2);//instantiating a merge stream inttemp = 0;//Receive content while((Temp=sis.read ())!=-1) {//Loop OutputOs.write (temp);//Save Content}
Java IO Learning