Io stream in JAVA

Source: Internet
Author: User

The IO stream in Java is used to process the data transfer from the device to the device before it is streamed in Java. Streams fall into two categories: byte stream and character stream.

BYTE stream: Inputstream,outputsteam. (The data in the computer is stored in bytes, and the byte stream can manipulate arbitrary data)

Character stream: Reader,writer. (character stream can only manipulate characters, but it is convenient to use it in actual application)

From the perspective of operation can be divided into: input and output streams.

The IO stream operation is divided into the following steps: 1. Importing IO stream packets, 2. Exception handling of IO stream, 3. Close IO stream to release resources.

BYTE stream

————————————————————————————————————————————————————————————————————————————————————————————————————————————

FileInputStream:

1 FileInputStream fis =new fileinputstram ("Xxx.text"); 2 int b;
Read standard notation 3while ((b=fis.read)!=-1) {4 System.out.println (b); 5 }6 fis.close ();

Fileoutputsteam:

FileOutputStream fos=New fileoutputstream ("Xxx.text",true);//fileoutputstream default every time you regenerate one " Xxx.text ", if you want to append, you should add a truefos.write (); Fos.close ();

Buffers in Java:

When reading and writing data, reading and writing an array at a time is significantly faster than one byte, so considering this design idea, the buffer is introduced. is actually the convection packaging. (Decoration design idea)

Bufferinputstream and Bufferoutputstream:

Bufferedinputstream bis=New bufferedinputstream (new Fileinputstrean ("Xxx.txt")); Bufferedoutputstream Bos=new bufferedoutputstream (new FileOutputStream ("Yyy.txt") byte []by=newbyte[1024x768]int  lenwhile (Len=bis.read (by))! = -1) {bos.write (arr,0, Len);} Bis.close ();//Only need to close bufferinputstreambos.close ();

Exception handling for streams below JDK 1.7:

It is important to note that the try catch cannot be performed while doing the exception handling of the IO stream. Because the IO stream is used to manipulate the underlying data, if a try catch is equivalent to hiding the IO problem and cannot be exposed, it is true that the IO exception is thrown.

Try{Bufferedinputstream bis=NewBufferedinputstream (NewFileinputstrean ("Xxx.txt")); Bufferedoutputstream Bos=NewBufferedoutputstream (NewFileOutputStream ("Yyy.txt")byte[]by=New byte[1024]intLen while(Len=bis.read (by))!=-1) {bos.write (arr),0, Len);}}finally{if(bis!=NULL) {bis.close ();//only need to close buffer}if(bos!=NULL) {bos.close ();}}

Exception handling for streams above JDK 1.7:

No manual shutdown is required and the program shuts down automatically.

Try (bufferinputstream bis=new bufferinputstream (new Fileinputstrean ("Xxx.txt")); Bufferoutputstream Bos=new bufferoutputstream (new FileOutputStream ("Yyy.txt")) { byte []by=newbyte[1024x768]int  lenwhile ((Len=bis.read) (by )){!=-1) {bos.write (arr,0, Len);}}

Character Stream

———————————————————————————————————————————————————————————————————————————————————— FileReader:

FileReader fr=New filereader ("Xxx.txt")int  C;  while ((C=fr.read ())!=-1) {   //read () method reads System.out.print ((char) c) by character size;} C.close ();

FileWriter:

FileWriter fw=New FileWriter ("Xxx.txt") fw.wrire ("Hello");  The Write () method automatically writes character bytes to Fw.close ();

A copy of a character stream:

FileReader fr=New filereader ("Xxx.txt"); FileWriter FW=new FileWriter ("Zzz,txt"); int C;  while ((C=fr.read ())!=-1) {fw.writer (c);} Fr.close (); Fw.close ();

Character stream and byte stream can be copied files, but do not recommend the use of characters to copy files, because when reading to convert the byte stream to a character stream, when writing to the character stream transposition of the word flow, too time-consuming!

When the program needs to read and write a piece of text, it is recommended to use a character stream, because it is read by code, will not appear half a Chinese situation, more accurate! Do not convert to byte stream when writing. It is important to note that only plain text can be copied when using a character stream copy. When the character is converted to bytes at the time of reading, the corresponding byte may not be found, and the byte loss occurs.

Custom byte array copy:

FileReader fr=New filereader ("Xxx.txt"); FileWriter FW=new FileWriter ("Zzz.txt"); Char []by=newchar[1024x768]; int Len;  while ((Len=fr.read (by))!=-1) {//The data on the file is transferred to the character array Fw.write (by,0, Len);//characters in the array go to the file}fr.close (); fw.close ();

byte array copy with buffering:

BufferedReader br=New bufferreader (new FileReader ("Xxx.txt")); BufferedWriter BW=new bufferwriter (new bufferwriter ("Zzz.txt")); int C  while ((C=br.read ())!=-1) {bw.writer (c);} Br.close (); Bw.close ();

Bufferreader and Bufferwriter:

BufferedReader br=New bufferreader (new FileReader ("Xxx.txt"));
BufferedWriter bw=new Bufferwriter (New FileWriter ("Zzz.txt")) String line; while ((Line=br.readline ()) =null) {reads a line of characters bw.write (lines);
Bw.nextline ();//newline}br.close ();
Bw.close ();

Simple application implements text inversion:

BufferedReader br=New bufferreader (new FileReader ("Xxx.txt")); String Line; ArrayList<string>list=new arraylist<>();  while ((Line=br.readline ()) =null) {list.add (line);}
Br.close ();

BufferedWriter br=New Bufferreader (new FileWriter ("Zzz.txt"));
for (int i=line.size (); i>=0;i--
Bw.close ();

The design pattern ———————————————————————————————————————————————————————————————————————————————————————— in IO stream

The source of design patterns: Design patterns originated from the construction industry, in order to let the object more capacity to promote, programmatic operation.

The essence of the decorative design pattern is to wrap the object so that he is more operational, and in the IO stream, buffer is a decoration. The advantage of the decorative design pattern is that the coupling is not strong, and the decoration class is not related to the change of decoration class.

Io stream in JAVA

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.