Java------IO (i)

Source: Internet
Author: User

Common base classes for IO streams:
Byte stream commonly used base class:
Inputstream,outputstream
Common base classes for character streams:
Reader Writer
Conversion Flow:
InputStreamReader: A bridge of byte flow to a character stream the character stream object that is used to manipulate the byte stream
OutputStreamWriter: A stream of characters flowing to a stream of bytes to manipulate a stream of characters


The subclass names derived from these four classes are the suffixes of their parent class names as subclasses






How to use character stream Reader&writer:

1. Create:
Taking FileReader FileWriter as an example
To create a FileReader object:

FileRead FR = new FileReader ("Demo.txt");

To create a FileWriter object:

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

The file is automatically created to the specified folder, and if there is a file with the same name, it will overwrite the original.


An exception occurs when you create it, and you need to handle the exception.


2. Read/write:
Fr.read () End flag: Return value is-1
Read one at a time, automatically read down
You can do this:


int len = 0;while (len = Fr.read ())! =-1) {System.out.println (len);//print out the result is a number that can be coerced}

Read an array at a time and read it automatically
You can do this:

Char buf[] = new Char[1024];int num = 0;while (num = Fr.read (BUF))! =-1) {System.out.println (new String (BUF));}

It's a lot easier to write.

Fw.write ("Shdsdhksldlksjdlksjdkl"); Fw.flush ();//refresh, brush content into destination


3. Close the character stream object:

Fr.close (); Fw.close ();

Be sure to determine if the object is created before you close it.


4. Handling of Exceptions:

Take FileWriter as an example, FileReader is the same.


FileWriter FW = null;try {fw = new FileWriter ("Demo.txt"); Fw.write ("Sssssssssssssssssss");} catch (IOException e) {//TODO Auto-generated catch Blocke.printstacktrace ();} Finally {try {if (fw! = null) {Fw.close ()}} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace (); }}


5. Use buffers to increase efficiency:
In order to improve the read efficiency of the stream, a stream object must be present before the buffer is created.


Create:
The constructor that passes the flow object that needs to be more efficient as a parameter to the buffer.

BufferedReader  bufr = new BufferedReader (New FileReader ("Demo.txt")); BufferedWriter BUFW = new BufferedWriter (New Filrwriter ("Demo.txt"));

Write/Read
Read:
Buffer new provides an efficient method ReadLine () read one line at a time


String line = Bufr.readline (); System.out.println (line);

Write:


Bufw.write ("DSADSADSADSADSAFRFG"); Bufw.flush ();

To close a buffer:
After you close the buffer, you do not have to close the character stream object again.


Bufr.close (); Bufw.close ();




How to use the byte stream:

1. Create:


FileOutputStream fos = new FileOutputStream ("Demo.txt");

will be automatically created and overwritten if a file with the same name is found.


FileInputStream fis = new FileInputStream ("Demo.txt");

2. Read/write
Write:


Fos.write ("Dsadsadsadwerfetrythy"); Fow.flush ();

Read:
Read one at a time

int len = 0;while (len = Fis.read ())! =-1) {System.out.println ((char) ch);}

Read one array at a time:

byte buf[] = new byte [1024];int len = 0;while (len = Fis.read (BUF))! =-1) {System.out.println (new String (Buf,0,len)); }


byte buf[] = new byte[fis.available ()];//estimated number of bytes int len = 0;while (len  = Fis.read (BUF))! =-1) {System.out.println (new String (Buf,0,len));}


4. Buffers
The constructor that will require the efficiency of the byte stream object to be passed as a parameter to the buffer.


Bufferedoutputstream Bufos = new Bufferedoutputstream (New FileOutputStream ("Demo.txt")); Bufferedinputstream Bufis = new Bufferedinputstream (New FileInputStream ("Demo.txt"));


5. Close
When there is a buffer, you just need to close the buffer just fine, no words call the Close method to close the stream object.



Conversion Flow:
InputStreamReader: A bridge of byte flow to a character stream the character stream object that is used to manipulate the byte stream
OutputStreamWriter: A stream of characters flowing to a stream of bytes to manipulate a stream of characters



1. Create:
Convert a byte stream to a character stream object, using the transform stream


FileInputStream fis = new FileInputStream ("Demo.txt"); InputStreamReader ISR = new InputStreamReader (FIS); Two sentences written in InputStreamReader ISR = new InputStreamReader (New FileInputStream ("Demo.txt"));



FileOutputStream fis = new FileOutputStream ("Demo.txt"); OutputStreamWriter OSW = new OutputStreamWriter (FIS); Two sentences written in outputstreamwriter OSW = new OutputStreamWriter (New FileOutputStream ("Demo.txt"));

2. Write/Read


Write:

Osw.write ("Sgydgasddsagukwe");

Read:


int len = 0;while (len = Isr.read ())! =-1) {System.out.print ((char) len);}

3. Buffers


FileInputStream fis = new FileInputStream ("Demo.txt"); InputStreamReader ISR = new InputStreamReader (FIS); BufferedReader bufr = new BufferedReader (ISR); three sentences: BufferedReader bufr = new BufferedReader (New InputStreamReader FileInputStream ("Demo.txt"));


BufferedWriter BUFW = new BufferedWriter (new OutputStreamWriter (New FileOutputStream ("Demo.txt"));


Java------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.