This article is to understand the basics of IO knowledge, this piece I looked for a few days, feeling and the previous period of time to learn a collection, scattered and important. Want to record this piece of learning process. So, the next few notes will be recorded, followed by a thorough summary. Because this piece is more important, so be sure to learn it well.
I. Overview of IO
IO: (Input Output)
---what is a stream:
A set of sequential, start-and-end data sets, such as file streams, network streams
---the difference between a stream and a file:
A file is a static method of storing data. A stream is a form of data at the time of transmission.
Java operates on data in a streaming way, and Java is used to manipulate the flow of objects in the IO package.
Flow by Operation data is divided into:
Byte stream (InputStream, outputstream) and character stream (Reader, Writer)
All the other classes of IO operations are derived from them.
All belong to the word stream is to InputStream, OutputStream for the end, such as FileInputStream, FileOutputStream, etc.
All belong to the character stream, is to Reader, Writer for the end such as FileReader, PrintWriter, etc.
Flow is divided by flow direction:
Input stream and output stream
Second, character Stream Writer
The structure of a character stream (only as an example of an output stream)
Writer--BufferedWriter
Chararraywriter
OutputStreamWriter--filewriter
Filterwriter//Filter Flow
PipedWriter//Pipe flow
StringWriter
//Example one creates a file, writes some contentStatic voidTest ()throwsioexception{Writer WR=NewFileWriter ("C:/1.txt");//to cover the original content.Wr.write ("This is a line of text"); Wr.write ("\ r \ n");//Write line breakWr.write ("This is the second line of text");//It's written in the buffer.Wr.flush ();//Flush BuffersWr.close ();//when you're finished with the flow, be sure to close } //FileWriter (String fileName, Boolean append) Static voidTest2 ()throwsioexception{Writer WR=NewFileWriter ("C:/1.txt",true);//The second argument can be passed true to indicate that it is written in a recoverable mannerWr.write ("This is what you write in Append mode"); //Wr.flush (); Have the following close, not flush here can alsoWr.close (); }
III. Standard IO exception handling
code example:
ImportJava.io.FileWriter;Importjava.io.IOException;ImportJava.io.Writer;Importjava.util.ArrayList;Importjava.util.List;//write a list to a fileclassTest16 { Public Static voidMain (string[] args) {List<String> NameList =NewArraylist<string>(); Namelist.add ("PHP"); Namelist.add ("Java"); Namelist.add ("C #"); Namelist.add ("C + +"); Writer WR=NULL; Try{WR=NewFileWriter ("list. exe"); for(String name:namelist) {wr.write (name+ "\ r \ n"); } } Catch(Exception ex) {Throw NewRuntimeException (ex); } finally { Try { if(WR! =NULL) {wr.close (); } } Catch(IOException ex) {Throw NewRuntimeException (ex); } } }}//key point, to close the resource in Finally, do not forget to determine if this resource is null
Four, character Stream Reader
example, reading data from a file
int read () reads a single character//returns the encoded value of this character
int read (char[] cbuf) reads the character into the array//The return value is the number of characters read, and if read at the end, returns 1 (1 This property is often used)
int read (char[] cbuf, int off, int len) reads the character into a portion of the array.
// read data from a file (this example is read in a character-by-character way) Static void throws IOException { Reader r=new filereader ("List of c:/. txt"); int ch=0; while ((Ch=r.read ())!=-1 ) { System.out.println ((char) ch); } }
// read data from a file and print it for a string of characters static void test5 () throws IOException {Reader R =new FileReader ("C:/1.txt" char [] buff=new char [1024]; int count=r.read (Buff); String str =new String (buff,0,count); System.out.println (str); R.close (); }
// example file copy, copy a file on C drive to E Drive
Such a copy is a copy of the character one, and it is clear that it is less efficient!
Reader r=New filereader ("E:\\fristone\\src\\myproject.java"); // Note: The path backslash is escaped Writer w=New FileWriter ("E:\\desc.txt"); int ch=0; while ((ch= r.read ())!=-1 ) { w.write (ch); } R.close (); W.close ()
Now that we have one copy, we think, how can we make the efficiency of replication more efficient?
We can define a container, use it to block a piece of replication, the efficiency will be higher. The following is a graphical description:
code example:
Reader r=New filereader ("E:\\fristone\\src\\myproject.java"); Writer W=new FileWriter ("E:\\desc.txt"); Char [] buff=newchar[1024]; // define a container // after the container is defined, some things happen, such as after several times, the rest of the capacity is less than the capacity of the defined container, so let it go once again there is bound to be space occupied. This is not what we want, so we lead the following parameter. int len=0; while ((len= r.read (Buff))!=-1 ) { w.write (buff,0, Len); } R.close (); W.close ();
There is really a lot of knowledge about IO, the next article to be continued ...
Java Foundation--io (i)