Java Learning Basics-I/O flow

Source: Internet
Author: User
Tags file copy

I. Classification of input and output streams

What is an input stream? What is an output stream? Insert a pipe into the file, and then start reading the data, if you stand at the angle of the file this is called the output, if you stand in the program angle this is called input. remember that the input and output streams are all from the point of view of the program.

Second, the node flow explanation

This type of file is used as a typical explanation of node flow.

Example: using the FileInputStream stream to read the contents of a Fileinputstream.java

Package cn.galc.test;import java.io.*; Public classTestfileinputstream { Public Static voidMain (String args[]) {intb =0;//Integer returned when calling the read () method using variable BFileInputStreaminch=NULL; //when you use the FileInputStream stream to read Chinese content, the read is garbled, because the read () method in the InputStream stream reads the content with a byte-by-byte read, and a Chinese character occupies two bytes,        So the reading of Chinese characters can not be displayed correctly. //FileReader in = null;//when you use the FileReader stream to read content, both Chinese and English are displayed correctly because the read () method inside the reader stream is read one character at a time, so that each read is a complete Chinese character, so that it can be displayed correctly.         Try {            inch=NewFileInputStream ("d:\\java\\myeclipse 10\\workspaces\\annotationtest\\src\\cn\\galc\\test\\fileinputstream.java"); //In = new FileReader ("D:/java/io/testfileinputstream.java");}Catch(FileNotFoundException e) {System. out. println ("The system cannot find the specified file! "); System.exit (-1);//Abnormal system Exit        }        Longnum =0;//use variable num to record the number of characters read        Try{//An exception is thrown when the read () method is called, so you need to catch the exception             while((b =inch. read ())! =-1) {                //when the int read () throws exception method is called, an integer of type int is returned//the condition for the end of the loop is to return a value of 1, indicating that the end of the file has been read at this point. //System.out.print (b + "T");//if "(char) B" is not used for conversion, then the directly printed B is the number, not English and Chinese.System. out. Print (Char) b); //"char (b)" Converts Chinese and English letters that use numbers to character inputnum++; }            inch. Close ();//close the input streamSystem. out. println (); System. out. println ("a total of read"+ num +"bytes of files"); } Catch(IOException E1) {System. out. println ("file read Error! "); }    }}

Example: Using a fileoutputstream stream to write data to a file

Package cn.galc.test;import java.io.*; Public classTestfileoutputstream { Public Static voidMain (String args[]) {intb =0; FileInputStreaminch=NULL; FileOutputStream out=NULL; Try {            inch=NewFileInputStream ("d:\\java\\myeclipse 10\\workspaces\\annotationtest\\src\\cn\\galc\\test\\mymouseadapter.java");  out=NewFileOutputStream ("D:/java/testfileoutputstream1.java"); //indicates the file to write the data to, and if a file such as Testfileoutputstream1.java does not exist in the specified path, the system automatically creates a             while((b =inch. read ())! =-1) {                 out. Write (b); //Call the Write (int c) method to write all the characters read to the specified file .            }            inch. Close ();  out. Close (); } Catch(FileNotFoundException e) {System. out. println ("file read failed"); System.exit (-1);//non-normal exit}Catch(IOException E1) {System. out. println ("file copy failed! "); System.exit (-1); } System. out. println ("the contents of the Testfileinputstream.java file have been successfully copied into the file Testfileoutstream1.java."); }}

Both FileInputStream and FileOutputStream are byte streams, both of which are input and output in the form of bytes. Therefore, the characters that occupy two bytes of storage space are displayed as garbled when they are read.

Example: Use FileWriter (character stream) to write data to the specified file.

Package cn.galc.test;/*Write data to a specified file using a filewriter (character stream) written in 1-character units*/import java.io.*; Public classtestfilewriter{ Public Static voidMain (String args[]) {/*using the FileWriter output stream to write data from a program to a Uicode.dat file using FileWriter flow to a file is written in one character at a time.*/FileWriter FW=NULL; Try{FW=NewFileWriter ("D:/java/uicode.dat"); //the nature of the character is an unsigned 16-bit integer//characters occupy 2 bytes inside the computer//this uses a for loop to output all the integers inside the 0~60000//It is equivalent to representing the whole world in the form of integers within the 0~60000 of all countries.                 for(intC=0; c<=60000; C + +) {fw.write (c); //use write (int c) to write an integer within the 0~60000 to the specified file//when I call the Write () method, I think that "(char) C" should be used in the execution of the cast, that is, converting integers to characters to display//because the file that writes the data can be seen, the data displayed in it is not an integer within the 0~60000, but rather a representation of the text in different countries.            }            /*Reads the contents of the specified file using the FileReader (character stream) read in a single character unit*/                intb =0; Longnum =0; FileReader FR=NULL; Fr=NewFileReader ("D:/java/uicode.dat");  while((b = Fr.read ())! =-1) {System. out. Print (Char) B +"\ t"); Num++; } System. out. println (); System. out. println ("a total of read"+num+"of characters"); }Catch(Exception e) {e.printstacktrace (); }    }}
Three, I/O flow summary

Java Learning Basics-I/O flow

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.