Input/output stream

Source: Internet
Author: User

After the file is more appropriate to appear is about the operation of the flow.

It is assumed that the write and write operation of data is essentially a binary number of 01 form, and the arrangement of these data is not confused. They are an orderly whole, and the captain's long expression of something called a stream. In this case, the stream is used to identify it.

Multiple stream classes are defined in the Java.io package for input/output functions, which can be categorized in different angles:

1, according to the direction of data flow into input and output.

2, according to the data processing unit divides the character stream and the character stream.

3, according to the different functions are divided into node flow and processing flow.

 PackageIopart;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException; Public classIopart { Public Static voidMain (string[] args) {/*** In the F:\tryFile directory, create a a.txt file, modify the contents of: Used to try the first demo of InputStream. */        Try {            //If you cannot find such a file in this path, it will be reported that the file did not find the exception, so use FileNotFoundException packageFileInputStream FileInputStream =NewFileInputStream (NewFile ("F:\\tryfile\\a.txt")); byte[] contents =New byte[1024]; Fileinputstream.read (contents);//There will be a read and write exception, and then from the file stream to the in-memory array to write data, there may be problems, you need to wrap a ioexceptionString result =NewString (contents);        SYSTEM.OUT.PRINTLN (result); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }    }}

Operation Result:

Used to try the first demo of InputStream.

This editor is really advanced, automatically erased the vast number of spaces behind me. In fact, we created an array of up to 1024 bite bits. So. How much to read, not to fill in the blanks. This will be a disadvantage, we want to store the content of the data is not certain size, then how much suitable for this byte?

Programme one:

 PackageIopart;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException; Public classIopart { Public Static voidMain (string[] args) {/*** In the F:\tryFile directory, create a a.txt file, modify the contents of: Used to try the first demo of InputStream. */        Try {            //If you cannot find such a file in this path, it will be reported that the file did not find the exception, so use FileNotFoundException packageFileInputStream FileInputStream =NewFileInputStream (NewFile ("F:\\tryfile\\a.txt")); byte[] contents = new byte [Fileinputstream.available ()]; Fileinputstream.read (contents);//There will be a read and write exception, and then from the file stream to the in-memory array to write data, there may be problems, you need to wrap a ioexceptionString result =NewString (contents);        SYSTEM.OUT.PRINTLN (result); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }    }}

The stream is to be closed after it is used as a resource.

 PackageIopart;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException; Public classIopart { Public Static voidMain (string[] args) {/*** In the F:\tryFile directory, create a a.txt file, modify the contents of: Used to try the first demo of InputStream. */FileInputStream FileInputStream=NULL; Try {            //If you cannot find such a file in this path, it will be reported that the file did not find the exception, so use FileNotFoundException packageFileInputStream =NewFileInputStream (NewFile ("F:\\tryfile\\a.txt")); byte[] contents =New byte[Fileinputstream.available ()]; Fileinputstream.read (contents);//There will be a read and write exception, and then from the file stream to the in-memory array to write data, there may be problems, you need to wrap a ioexceptionString result =NewString (contents);        SYSTEM.OUT.PRINTLN (result); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }finally {            if(fileinputstream!=NULL){                Try{fileinputstream.close (); } Catch(IOException e) {E.printstacktrace (); }            }        }    }}

This kind of extraction form, is still more important.

Way two:

 PackageIopart;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException; Public classIopart { Public Static voidMain (string[] args) {Iopart Iopart=NewIopart ();            IOPART.METHOD2 (); }    Private voidmethod2 () {FileInputStream FileInputStream=NULL; Try{FileInputStream=NewFileInputStream (NewFile ("F:\\tryfile\\sendpart.java")); byte[] contents =New byte[1024]; intLength = 0;  while((Length=fileinputstream.read (contents))!=-1) {System.out.println (NewString (contents,0, length)); }        } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        finally {            if(fileinputstream!=NULL){                Try{fileinputstream.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }        }    }    Private voidmethod1 () {/*** In the F:\tryFile directory, create a a.txt file, modify the contents of: Used to try the first demo of InputStream. */FileInputStream FileInputStream=NULL; Try {            //If you cannot find such a file in this path, it will be reported that the file did not find the exception, so use FileNotFoundException packageFileInputStream =NewFileInputStream (NewFile ("F:\\tryfile\\a.txt")); byte[] contents =New byte[Fileinputstream.available ()]; Fileinputstream.read (contents);//There will be a read and write exception, and then from the file stream to the in-memory array to write data, there may be problems, you need to wrap a ioexceptionString result =NewString (contents);        SYSTEM.OUT.PRINTLN (result); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }finally {            if(fileinputstream!=NULL){                Try{fileinputstream.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }        }    }}

Write:

 PackageIopart;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException; Public classOutputDemo1 { Public Static voidMain (string[] args) {FileOutputStream FileOutputStream=NULL; Try{FileOutputStream=NewFileOutputStream (NewFile ("F:\\tryfile\\output.txt")); String contentstring=NewString ("Lifei");            Fileoutputstream.write (Contentstring.getbytes ()); System.out.println ("Write Success"); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally {            if(fileoutputstream!=NULL){                Try{fileoutputstream.close (); } Catch(IOException e) {e.printstacktrace (); }            }        }    }}

Input/output stream

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.