Java--io

Source: Internet
Author: User

After a day of study, the basic has a certain understanding of Java--io, and now share with you.

Java. The IO package defines the types of multiple streams (classes or abstract classes) to implement input and output functions, which can be categorized from different angles:

Can be divided into input stream and output stream according to different direction of data flow

Can be divided into byte stream and character stream according to the different units of processing data

According to the different functions can be divided into node stream and processing flow

is the classification of the Java--io stream; see, you must be asking what the input and output streams are distinguished by, the input and output streams are measured in terms of the program.

Gossip less talk, now straight to the topic:

Read the contents of the specified file (read by byte stream)

 Public classTestfileinputstream {/**file input stream (byte stream input) *@paramargs*/     Public Static voidMain (string[] args) {FileInputStream input=NULL; intb = 0; Try{input=NewFileInputStream ("e:\\jsp\\jsp_demo001\\index.jsp"); } Catch(FileNotFoundException e) {System.out.println ("File does not exist"); System.exit (0); }        Longnum = 0; Try {             while((B=input.read ())!=-1) {System.out.print (Char) b); Num++;            } input.close ();            System.out.println (); System.out.println ("Total read:" +num+ "bytes"); } Catch(IOException e) {e.printstacktrace (); }    }}

Code is not difficult, if you have any questions please leave a message.

Second, read the contents of the specified file (read in a character stream)

 Public classFileReader {/**file input stream (character stream input) *@paramargs*/     Public Static voidMain (string[] args) {FileReader input=NULL; intb = 0; Try{input=NewFileReader ("e:\\jsp\\jsp_demo001\\index.jsp"); } Catch(FileNotFoundException e) {System.out.println ("File does not exist"); System.exit (0); }        Longnum = 0; Try {             while((B=input.read ())!=-1) {System.out.print (Char) b); Num++;            } input.close ();            System.out.println (); System.out.println ("Total read:" +num+ "bytes"); } Catch(IOException e) {e.printstacktrace (); }    }}

Here to illustrate a point, the difference between the byte stream and the character stream, the byte stream is a character to read a character, the character stream is different, it is a character to read one character;

Third, the file output stream (in bytes)

 Public classTestfileoutputstream {/**file output stream (Byte stream) *@paramargs*/     Public Static voidMain (string[] args) {intb = 0; FileInputStream input=NULL; FileOutputStream Output=NULL; Try{input=NewFileInputStream ("e:\\jsp\\jsp_demo001\\index.jsp"); //If the file does not exist under the specified path, a file is automatically created for USOutput =NewFileOutputStream ("e:\\jsp\\jsp_demo001\\index1.jsp"); //set to True to copy under the specified file without deleting the original file//output = new FileOutputStream ("e:\\jsp\\jsp_demo001\\index1.jsp", true);}Catch(FileNotFoundException e) {System.out.println ("The specified file does not exist"); System.exit (0); }        Try {             while((B=input.read ())!=-1) {Output.write (Char) b);            } input.close ();        Output.close (); } Catch(IOException e) {System.out.println ("File copy Error"); System.exit (0); } System.out.println ("File copy Complete"); }}

Iv. file output stream (in characters)

 Public classFilewriter {/**file input stream (character stream output) *@paramargs*/     Public Static voidMain (string[] args) {FileWriter output=NULL; Try{Output=NewFileWriter ("E:\\jsp\\jsp_demo001\\cn.txt"); } Catch(IOException e) {System.out.println ("There is a problem with the file path"); System.exit (0); }        Try {                 for(inti=0; i<50000; i++) {output.write (i);            } output.close (); System.out.println ("Save Complete"); } Catch(IOException e) {e.printstacktrace (); }    }}

Finally, we introduce a video file read and save:

 Public classFileoutputstream_media {/**file output stream (Byte stream) *@paramargs*/     Public Static voidMain (string[] args) {intb = 0; FileInputStream input=NULL; FileOutputStream Output=NULL; Try{input=NewFileInputStream ("E:\\jsp\\jsp_demo001\\move.avi"); //If the file does not exist under the specified path, a file is automatically created for USOutput =NewFileOutputStream ("E:\\jsp\\jsp_demo001\\move1.avi"); } Catch(FileNotFoundException e) {System.out.println ("The specified file does not exist"); System.exit (0); }        Try {             while((B=input.read ())!=-1) {Output.write (Char) b);            } input.close ();        Output.close (); } Catch(IOException e) {System.out.println ("File copy Error"); System.exit (0); } System.out.println ("File copy Complete"); }}

I do not like long-winded text, like this direct complete code, of course, every place above I have made comments, if there is any doubt in your learning process, please leave a message, I will give you an answer as soon as possible.

In this statement: If the above content is inappropriate, please leave a message, thank you.

Java--io

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.