IO Flow Summary

Source: Internet
Author: User

The IO stream is used for input and output, which is to output data from the program to the local hard disk and read data from the local hard drive to the program. Not much nonsense to say, directly on the diagram

Input of a byte stream using Fileinputsteam

public static void Main (string[] args) throws IOException {//Creates a file source, reads the data from this file, and prints file File=new file ("e:/src/ ByteOut.txt ");//new a FileInputStream class with a parameter of file that reads the data in this file InputStream in=new fileinputstream (file);//Create length int len=0 ;//Create Intermediate container byte[] By=new byte[1024];//The following call the read () method to start reading data, if you have read the data, no data can be read, will return -1//that is Len=-1 then jump out of the loop while ( -1!= (len= In.read (by)) {System.out.println, New String (by);} Close connection In.close ();}

Output of byte stream using FileOutputStream

public static void Main (string[] args) throws IOException {//Creates a file source that writes data to this file in the filename file=new file ("E:/src/byteout.txt" );//new a FileInputStream class with a parameter of File,true representing an additional outputstream out=new FileOutputStream (file,true); String str= "This is test";//convert str to byte array byte[] B=str.getbytes ();//Call the Write method to output the data. Out.write (b);//close connection out.close ();}

The input of a two-character stream (a stream of characters can handle only plain text, and a byte stream handles all data)

Using FileReader

public static void Main (string[] args) throws IOException {//Creates a file source that writes data to this file in the filename file=new file ("E:/src/byteout.txt" );//new a FileInputStream class with parameters of File,reader in=new filereader (file);//Create length int len=0;//Create intermediate container because it is a character, All resumes a char-type array used as an intermediate container char[] by=new char[1024];//The Read () method is called below and reads the data into the by array, and if the data has been read, no data can be read, which will return -1//that is len=- 1 then jump out of the loop while ( -1!= (Len=in.read)) {System.out.println (new String);} Close connection In.close ();}

Output of a character stream

public static void Main (string[] args) throws IOException {//Creates a file source that writes data to this file in the filename file=new file ("E:/src/byteout.txt" );//new a FileInputStream class with a parameter of File,true representing the appended writer Out=new FileWriter (file,true); String str= "This is test";//Call the Write method to write data to. Out.write (str);//close connection out.close ();}

The three Bufferedinputstream and Bufferedoutputstream classes are character buffer streams, in order to improve performance and efficiency,

1. Java.io.BufferedInputStream and Java.io.BufferedOutputStream can increase the buffer function for the Inputstream,outputstream class. When building a Bufferedinputstream instance, you need to give an instance of the InputStream type, and finally implement the InputStream instance when Bufferedinputstream is implemented. Similarly, when building bufferedoutputstream, it is also necessary to give a OutputStream instance when implementing Bufferedoutputstream, in fact, the OutputStream instance is implemented finally.

2. Bufferedinputstream's data member buf is a bit array, which defaults to 2048 bytes. When reading a data source, such as a file, Bufferedinputstream tries to fill the buf as much as possible. When using the Read () method, the data in the BUF is actually read first rather than directly to the source of the data. When the data in the BUF is insufficient, Bufferedinputstream will then implement the read () method of the given InputStream object, extracting the data from the specified appliance.

3. Bufferedoutputstream's data member Buf is also a bit array, which defaults to 512 bytes. When writing data using the Write () method, the data is actually written to buf, and the Write () method of the given OutputStream object is implemented when the BUF is full, and the BUF data is written to the destination, rather than the action of writing to the destination every time.

The following usage is the same as the input and output of the previous byte stream and the character stream.

Bufferedoutputstream bos=new Bufferedoutputstream (New FileOutputStream (New File ("E:/src/a.txt"));

Bufferedinputstream bis=new Bufferedinputstream (New FileInputStream (New File ("E:/src/a.txt"));

Four-byte flow to the class used for the character stream BufferedReader and InputStreamReader both can be just a difference, that is, the InputStreamReader class is to turn bytes into a character array

The BufferedReader ReadLine () method can stream bytes to a string and InputStreamReader only the Read () method returns char[];

public static void Main (string[] args) throws IOException {//Create file File=new files ("E:/src/byteout.txt");// New an instance of the BufferedReader class class that contains InputStreamReader  can specify the encoding BufferedReader br=new BufferedReader (New InputStreamReader (New FileInputStream (file), "UTF-8")); String Str=br.readline (); System.out.println (str); Br.close ();}

InputStreamReader usage

public static void Main (string[] args) throws IOException {//Create file File=new files ("E:/src/byteout.txt");// New A InputStreamReader class  can specify the encoding InputStreamReader isr=new InputStreamReader (new FileInputStream (file), "UTF-8") ;//Create Intermediate container char[] c=new char[1024];while ( -1!= (Isr.read (c))) {System.out.println (new String (c));} Isr.close ();}

Five-byte array stream bytearrayinputstream Bytearrayoutputstream

The Bytearrayoutputstream class is a buffer that creates a byte type array inside the program when it creates an instance of it. The byte type data is then written to or read from the array using instances of Bytearrayoutputstream and Bytearrayinputstream. In the network transmission we often have to transfer a lot of variables, we can use Bytearrayoutputstream to collect all the variables together, and then send the data at once. The specific usage is as follows:

Bytearrayoutputstream: Can capture data from memory buffers, convert to byte arrays, Bytearrayinputstream: You can convert a byte array into an input stream

public class Outin {public static void main (string[] args) throws IOException {outbyte ("E:/src/mhy.txt", Inbyte ("e:/src/ My.txt "));} Test Bytearrayinputstream and output to e:/src/mhy.txtpublic static void Outbyte (String src,byte[] b) throws Ioexception{file File =new File (SRC); byte[] Best=new byte[1024];outputstream out=new bufferedoutputstream (New FileOutputStream (File));// New one Bufferedinputstream This can use polymorphic InputStream in=new bufferedinputstream (new Bytearrayinputstream (b)); int len=0;// Read the data to best and output while ( -1!= (Len=in.read)) {out.write (best); System.out.println (New String (best));} Out.flush (); In.close (); Out.close ();} Test Bytearrayoutputstream and return a byte[] array public static byte[] Inbyte (String src) throws Ioexception{file file=new File ( SRC); byte[] Best=new byte[1024];inputstream inp=new bufferedinputstream (new FileInputStream (file));//new A Bytearrayoutputstream This cannot be used polymorphic Bytearrayoutputstream out=new bytearrayoutputstream (); int Len=0;while ( -1!= (len= Inp.read (best)) {//writes byte array to Outout.write (best, 0, Len);} Assign valid information in out to Bestbest=out.tobytearray (); Out.flush (); Inp.close (); Out.close (); return best;}}

Summarize:

 

IO Flow Summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.