For any programming language, the input-output (I/O) system is more complex and more central. The relevant APIs are available in the java.io. Package.
Concept division in Java streaming
Direction of Flow:
-
- Input stream: Data source to program (Inputstream,reader read in)
- Output stream: Program to Destination (Outputstream,writer write out)
Working with Data units:
-
- BYTE stream: Reads data by bytes (Inputstream,outputstream)
- Character stream: Reading data by characters (Reader,writer)
Different functions
-
- Node stream: Read and write data directly from a data source or destination
- Processing flow: Not directly connected to the data source or destination, is the flow of processing flow, child labor on other flows to improve the performance of the program.
The stream provided by the JDK inherits four classes: InputStream (byte input stream), OutputStream (byte output stream), Reader (character input stream), Writer (character output stream).
The following are the common streams in IO in Java.
InputStream
Abstract class Java.io.InputStream is the parent class of all byte input stream types, which defines the basic method of reading data in bytes and is differentiated and implemented in its subclasses.
Three basic read methods:
- int read ()
- int read (byte[] buffer)
- int read (byte[] buffer,int offset,int length)
Other methods:
- void Close ()
- int available ()
- Skip (long N)
- Boolean marksupported ()
InputStream class level
Import java.io.File; import java.io.InputStream; import java.io.FileInputStream;p ublic class inputstreamdemo{ public static void Main (String args[]) throws exception{ //exception thrown, non-processing //1th step, using the file class to find a files f= new ("D : "+ file.separator +" test.txt "); Declares a File object //2nd step, instantiates a parent class object through a subclass inputstream input = null; Prepare an input object of type = new FileInputStream (f) ; Through object polymorphism, instantiate //3rd step, read operation byte b[] = new byte[1024]; The size of the array is determined by the file int len = 0; int temp = 0; Receive each read-in data while ((Temp=input.read ())!=-1) { //indicates that there is still content, the file is not finished reading B[len] = (byte) temp; len++; } 4th step, close the output stream input.close (); Turn off the output stream System.out.println ("content:" + new String (B,0,len)); Change byte array to string output }};
OutputStream
Java.io.OutputStream corresponds to Java.io.InputStream, which is the abstract parent class for all byte output stream types.
Three basic write methods:
- void write (int c)
- void Write (byte[] buffer)
- void Write (byte[] buffer,int offset,int length)
Other methods:
- void Close ()
- void Flush ()
OutputStream class level
Import java.io.File; import java.io.OutputStream; import java.io.FileOutputStream;p ublic class outputstreamdemo{ public static void Main (String args[]) throws exception{ //exception thrown, non-processing //1th step, using the file class to find a files f= new ("D : "+ file.separator +" test.txt "); Declares a File object //2nd step, instantiates a parent class object through a subclass outputstream out = null; Prepare an Output object out = new FileOutputStream (f) ; Instantiate //3rd step, write the String str = "Hello world!!!"; Prepare a string byte b[] = Str.getbytes (); Only a byte array can be output, so the string becomes a byte array out.write (b); Write Data //4th step, turn off output stream //Out.close (); Close output stream }};
Reader
Abstract class Java.io.Reader is the parent class for all character input stream types, which declares a method for reading a stream of characters.
Three basic read methods:
- int read ()
- int read (char[] cbuf)
- int read (char[] cbuf,int offset,int length)
Other methods:
- void Close ()
- Boolean Ready ()
- Skip (long N)
- Boolean marksupported ()
- void mark (int readaheadlimit)
- void Reset ()
Reader class hierarchy
Import java.io.File; import java.io.Reader; import Java.io.FileReader;p ublic class readerdemo{public static void Ma In (String args[]) throws exception{ //exception throw, do not process //1th step, use the file class to find a files f= new ("D:" + File.separator + "Test.txt"); Declares a File object //2nd step, instantiates a parent class object through a subclass Reader input = null; Prepare an input object of type = new FileReader (f) ; Through the object polymorphism, the instantiation //3rd step, read operation char c[] = new char[1024]; All the contents are read into this array , int temp = 0; Receive each content int len = 0; Read the content while ((Temp=input.read ())!=-1) { //If not-1 means there is still content, you can continue to read C[len] = (char) temp; len++; } 4th step, close the output stream input.close (); Turn off the output stream System.out.println ("content:" + new String (C,0,len)); Convert a character array into a string output }};
Writer
Java.io.Writer corresponds to the Java.io.Reader class, which is the common parent of all character output stream types.
Five Basic Write methods:
- void write (int c)
- void Write (char[] cbuf)
- void Write (char[] cbuf,int offset,int Leng)
- void Write (String string)
- void Write (String string,int offset,int length)
Other methods:
- void Close ()
- void Flush ()
Writer class level
Import java.io.File; import java.io.Writer; import java.io.FileWriter;p ublic class writerdemo{public static void Ma In (String args[]) throws exception{ //exception throw, do not process //1th step, use the file class to find a files f= new ("D:" + File.separator + "Test.txt"); Declares a File object //2nd step, instantiates a parent class object through a subclass Writer out = null; Prepare an Output object out = new FileWriter (f) ; Instantiation //3rd step, write operation via object polymorphism String str = "Hello world!!!"; Prepares a string out.write (str); Outputs the content, saves the file //4th step, closes the output stream Out.flush (); Mandatory emptying of the contents of the buffer //Out.close (); At this point, it is not closed }};
Copy instance:
Import java.io.*;p ublic class copy{public static void Main (String args[]) {if (args.length!=2) {//Determine if it is Two parameter System.out.println ("the input parameter is incorrect.) ") ; System.out.println ("Example: Java Copy source file path destination file path"); System.exit (1); System Exit} File F1 = new file (Args[0]); The file object of the source files F2 = new file (args[1]); File Object if (!f1.exists ()) {System.out.println ("source file does not exist! ") ; System.exit (1); } InputStream input = null; Prepare the input stream object, read the source file outputstream out = null; Prepare the output stream object, write to the target file try{input = new FileInputStream (F1); }catch (FileNotFoundException e) {e.printstacktrace (); } try{out = new FileOutputStream (F2); }catch (FileNotFoundException e) {e.printstacktrace (); } if (Input!=null && out!=null) {//determines if the input or output is ready for int temp = 0; try{ while ((Temp=input.read ())!=-1) {//Start copy out.write (temp); Read Side Write} System.out.println ("Copy done!") ") ; }catch (IOException e) {e.printstacktrace (); System.out.println ("Copy failed! ") ; } try{Input.close (); Close Out.close (); Close}catch (IOException e) {e.printstacktrace (); } } } }
* * * in the general operation of input and output will need to use a byte or character stream, but sometimes you need to change the character stream into the form of a byte stream, or the byte stream into a character stream form, so you need another set of conversion flow operation class
-
- OutputStreamWriter: Is the writer's subclass, which converts the output character stream into a byte stream.
- InputStreamReader: is a subclass of reader that converts the input byte stream into a character stream.
Conversion steps:
If you take a file operation as an example, the total character data in memory needs to be saved in the file by OutputStreamWriter into a byte stream, and the read-in byte stream needs to be inputstreamreader into a character stream when read.
For example, the file output stream of a byte is output as a character
Import java.io.*;p ublic class outputstreamwriterdemo{public static void Main (String args[]) throws Exception { //All exceptions thrown File F = new file ("D:" + File.separator + "test.txt"); Writer out = null; Character output stream out = new OutputStreamWriter (new FileOutputStream (f)); The byte stream becomes a character stream out.write ("Hello world!!"); Use character stream output out.close (); }};
You can also read a byte stream file in the form of a character stream when you read it
Import java.io.*;p ublic class inputstreamreaderdemo01{public static void Main (String args[]) throws exception{ file F = new file ("D:" + File.separator + "test.txt"); Reader reader = null; reader = new InputStreamReader (new FileInputStream (f)); Converts a byte stream into a character stream char c[] = new char[1024]; int len = Reader.read (c); Read reader.close (); Close System.out.println (new String (C,0,len));} ;
Original: http://www.cnblogs.com/oumyye/p/4314412.html
Java Fundamentals---I/o technology