Dark Horse programmer------IO (i)

Source: Internet
Author: User

Dark Horse programmer------IO (i)

1.1 IO (Input Output) Flow Overview:

IO streams are used to process data transfer between devices

The Java operation of the data is streamed through the way
Java is used to manipulate the flow of objects in the IO packet stream by operation data are divided into two types: byte stream and character stream.
Flow is divided into: input stream, output stream.

Reads data from the peripheral into memory: input.

Writes the number of memory to the peripheral: output.

Character stream and byte stream in 1.2 io:

Two base classes of byte stream:
InputStream OutputStream

Two base classes of character streams:
Reader Writer

P.S.
The subclass names derived from these four classes are the suffixes whose parent class name is the subclass name.
such as: InputStream sub-class FileInputStream.
such as: Reader Subclass FileReader.

Requirement: On the hard disk, create a file and write some text data (example 1).

1 Example 1:2 ImportJava.io.*;3 classFilewriterdemo4 {5      Public Static voidMain (string[] args)throwsIOException6     {7         //creates a FileWriter object. When the object is initialized, it must explicitly manipulate the file. 8         //and the file is created in the specified directory. If the directory already has a file with the same name, it will be overwritten. 9         //in fact, the step is to specify the destination of the data to be stored. TenFileWriter FW =NewFileWriter ("Demo.txt"); One  A         //call the Write method to write the string to the stream.  -Fw.write ("ABCDE"); -  the         //refreshes the data in the buffer in the Stream object.  -         //brushes the data to the destination.  -         //Fw.flush (); -  +  -         //The stream resource is closed, but the data in the internal buffer is refreshed once before it is closed.  +         //brushes the data to the destination.  A         //and flush difference: After flush refreshes, the stream can continue to be used, and when close is refreshed, the stream is closed.  at fw.close (); -     } -}

Ps:1. The Close method can only be used once.

2. After the stream is closed, you cannot call the Write method again, otherwise the following exception error is reported:

3. If you add true to the constructor, you can implement the continuation of the file (example 2)

1 Example 2:2 ImportJava.io.*;3 classFileWriterDemo34 {5      Public Static voidMain (string[] args)throwsIOException6     {7 8         //pass a true parameter, which means that the existing file is not overwritten. And the data is resumed at the end of the existing document. 9FileWriter FW =NewFileWriter ("Demo.txt",true), depending on the given file name and whether the data is appended to theBooleanValue to construct the FileWriter object. Fortrue, the data is written at the end of the file, not at the beginning of the fileTen  OneFw.write ("Nihao\r\nxiexie");//\ r \ n means line break A  - fw.close (); -     } the}

1.3 IO Exception Handling (example 3): To prevent a code exception from being closed, the stream is closed in finally

1 Example 32 ImportJava.io.*;3 4 classFileWriterDemo25 {6      Public Static voidMain (string[] args)7     {8FileWriter FW =NULL;//Create a reference outside9         TryTen         { OneFW =NewFileWriter ("Demo.txt");//initialize in the inside AFw.write ("ABCDEFG"); -  -         } the         Catch(IOException e) -         { -System.out.println ("Catch:" +e.tostring ()); -         } +         finally -         { +             Try A             { at                 if(fw!=NULL) - Fw.close ();  -             } -             Catch(IOException e) -             { - System.out.println (e.tostring ()); in             } -              to         }         +  -     } the}

Requirement: Reads a document and prints the characters read to the console. (using FileReader)
First read: Reading text file data using the Read () method (example 4)

1 Example 4:2 3 classFilereaderdemo4 {5      Public Static voidMain (string[] args)throwsIOException6     {7         //creates a file that reads the stream object, and associates the file with the specified name. 8         //to ensure that the file is already present, an exception will occur if it does not exist FileNotFoundException9FileReader FR =NewFileReader ("Demo.txt");Ten  One         //invokes the Read method that reads the stream object.  A         //Read (): read one character at a time. and will automatically read down.  -          -         intCH = 0; the  -          while((Ch=fr.read ())!=-1) -         { -System.out.println ((Char) ch); +         } -  +  A         /* at While (true) -         { - int ch = fr.read (); - if (ch==-1) - Break ; - System.out.println ("ch=" + (char) ch); in         } -         */ to  +  -  the fr.close (); *  $ Panax Notoginseng     } -}

Second read: Use the Read (char[]) method to read the text file data (example 5).

1 Example 52 3 4 ImportJava.io.*;5 6 classFileReaderDemo27 {8      Public Static voidMain (string[] args)throwsIOException9     {TenFileReader FR =NewFileReader ("Demo.txt"); One          A  -         //defines a character array. Used to store read-to characters.  -         //the Read (char[]) returns the number of characters.  the         Char[] buf =New Char[1024]; -  -         intnum = 0; -          while((Num=fr.read (BUF))!=-1) +         { -System.out.println (NewString (buf,0, num)); +         } A          at  - fr.close (); -     } -}

Dark Horse programmer------IO (i)

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.