IO (Input Output) stream ___ character stream, byte stream

Source: Internet
Author: User

I. Overview of IO flow------ for manipulating data

IO streams are used to process data transfer between devices;

Java operation of data is through the flow of the way;

Java is used to manipulate the flow of objects are in the IO package;

The flow is divided into: byte Stream (Universal) and character stream, by flow direction: input stream and output stream.

Abstract base class for byte stream:

   InputStream, OutputStream

Abstract base class for character streams:

  Reader, Writer

Note: The subclass names derived from these four classes are the suffixes whose parent class name is the child class name.

such as: InputStream sub-class FileInputStream; reader's subclass Filereaderstream.

Two

Two base classes for byte stream:

InputStream, OutputStream

Two base classes for character streams: (specialized in text and data)

Reader, Writer

--------->>> Character Stream

First, the operation of the main file to demonstrate

Requirements: On the hard disk, create a file and write some text data

Found a writer subclass object specifically for manipulating files. FileWriter. The suffix name is the parent class name. The prefix is the function of the stream object.

  

Creates a FileWriter object that must be explicitly manipulated when initialized;

And the file will be created to the specified directory, if the directory has a file with the same name, the file is overwritten;

In fact, the step is to specify the destination of the data to be stored:

              FileWriter fw = new FileWriter("demo.txt");

Call the Write method to write the string to the stream:

Fw. writer ("abcdef");

Refreshes the buffered data in the stream object and brushes the data to the destination:

Fw. Flush ();

Closes the stream resource, but flushes the internal buffer's data before closing, flushing the data to the destination.

and flush difference: After flush refreshes, the stream can continue to be used, and when close refreshes, the stream is closed:

Fw. Close ();

How IO exceptions are handled:

Please look at the code:

ImportJava.io.*; CALSS Filewriterdemo { Public Static voidMain (string[] args) {FileWriter fw=NULL; Try{FW=NewFileWriter ("Demo.txt"); Fw.write ("SAFDASF"); }catch(IOException) {
System.out.println ("Catch:" +e.tostring ());
}
    finally {
      Try {
if (fw!=null)
Fw.close ();
} catch(IOException) {
System.out.println ("Catch:" +e.tostring ());
}
}
}
}

  

Continuation of the document:

Pass a true parameter, which means that the existing file is not overwritten and the data is resumed at the end of the existing file:

FileWriter FW = new FileWriter ("Demo.txt", true);

Read the text file:

   Way One:

      Creates a file read stream object, associated with a file of the specified name;

To ensure that the file is already present, an exception filenotfoundexception occurs if it does not exist.

FileReader fw = new FileReader ("Demo.txt");

Call the Read method that reads the Stream object:

Read (): reads one character at a time and automatically reads it down.

while ((ch = fr.read ())! = -1) {

System.out.println ("ch=" + (char) ch);

}

  Way two:

FileReader fw = new FileReader ("Demo.txt");

/* Defines an array of characters for storing read characters;

The read (char[]) returns the number of characters */:

         char[] buf = new char[1024];

int num = 0;

while ((num=fr.read (buf)) ! =-1) {

System.out.println (New String (buf, 0, num));

}

Little exercise: Read a. java file and print it on the console.

 Copy text file:

Copy the C drive one file text to the D drive:

The principle of replication is to store the file data from the C drive in a file on the D drive.

Steps:

1. Create a file in the D drive to store the data in the C drive file.

2. Define read stream and C-Drive file association;

3, through continuous reading and writing to complete the data storage;

4. Close Resources

A copy class that implements the replication feature:

1  Public Static voidCopy () {2FileWriter FW =NULL;3FileReader FR =NULL;4   Try {5FW =NewFileWriter ("Demo_copy.txt");6FR =NewFileReader ("Demo.java");7      Char[] buf =New Char[1024];8      intLen = 0;9       while(len = Fr.read (BUF))! =-1) {TenFw.write (buf, 0, Len); One      } A}Catch(IOException e) { -        Throw NewRuntimeException ("Read and Write Failed"); -     } the     finally { -         if(FR! =NULL) -           Try { - fr.close (); +}Catch(IOException e) { -  +            } A           if(FW! =NULL) at           Try { - fw.close (); -}Catch(IOException e) { -  -            } -     } in}

    

IO (Input Output) stream ___ character stream, byte 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.