Summary of IO streams in Java

Source: Internet
Author: User

Java中的IO流总结

1. The inheritance relationship of the stream, as well as the byte stream and character stream.
2. Node stream FileOutputStream and FileInputStream and process flow bufferedinputstream and Bufferedoutputstream. and the corresponding fileoutputwriter,fileinputreader,bufferedinputreader,bufferedoutputwriter.
3. Convert Stream InputStreamReader and OutputStreamWriter

一:流的继承关系

BYTE stream

Character Stream

The use of character streams and byte streams: Byte streams are typically used to process images, videos, and Ppt,word types of files. Character streams are typically used to work with plain text types of files, such as TXT files, which can be used to process plain text files, but character streams cannot be used to process non-text types of files such as image video.

二:处理流BufferedReader,BufferedWriter,BufferedInputStream, Bufferedoutputsstream, will be wrapped up a layer of node stream. That is, the processing flow is based on the node flow, with buffered flow, also known as the buffer stream, the buffer stream processing file input and output speed is the fastest. So the general buffer flow is used more.

Here are two simple examples of File replication:

1  Public classMycopytest {2      Public Static voidMain (string[] args) {3File src =NewFile ("D:/1.jpg");4         //d:/1.jpg must exist or it will be an error.5File dest =NewFile ("D:/2.jpg");6         //Overwrite if d:/2.jpg exists, new if not present7 streamcopy (SRC, dest);8     }9 Private Static voidreadcopy (File src,file dest)Ten     { OneFileReader fr=NULL; AFileWriter fw=NULL; -BufferedReader br=NULL; -BufferedWriter bw=NULL; the         Try { -HrsNewFileReader (SRC); -fw=NewFileWriter (dest); -Br=NewBufferedReader (FR); +bw=NewBufferedWriter (FW); - String str; +              while((Str=br.readline ())! =NULL) A             { at bw.write (str); - bw.newline (); -             } -}Catch(IOException e) { -             //TODO auto-generated Catch block - e.printstacktrace (); in         } -         Try { to bw.close (); + br.close (); -}Catch(IOException e) { the             //TODO auto-generated Catch block * e.printstacktrace (); $         }Panax Notoginseng  -     } the     Private Static voidstreamcopy (file src, file dest) { +FileInputStream FIS =NULL; AFileOutputStream fos =NULL; theBufferedinputstream bis =NULL; +Bufferedoutputstream BOS =NULL; -         Try { $FIS =NewFileInputStream (SRC); $FOS =NewFileOutputStream (dest); -bis =NewBufferedinputstream (FIS); -BOS =NewBufferedoutputstream (FOS); the             intLen; -             byte[] B =New byte[1024];Wuyi              while(len = Bis.read (b))! =-1) { theBos.write (b, 0, Len); -                 //Bos.write (B,0,len): Writes the size bytes read to the array Wu                 //Bos.write (b); last time if the array is not full, it will read more.  -             } About}Catch(IOException e) { $             //TODO auto-generated Catch block - e.printstacktrace (); -         } -         Try { A bos.close (); + bis.close (); the}Catch(Exception e) { -             //TODO auto-generated Catch block $ e.printstacktrace (); the         } the     } the}
三:转换流InputStreamReader和OutputStreamWriter

The function of the conversion stream, the text file in the hard disk in the form of byte stream storage, through the InputStreamReader read after the conversion to a character stream to the program processing, the program handles the character stream through OutputStreamWriter conversion to a byte stream save.

1  Public classInputstreamwritertest {2      Public Static voidMain (string[] args) {3File src =NewFile ("d:/files/bullshit. txt");4File dest =NewFile ("d:/files/Smith. txt");5BufferedWriter BW =NULL;6BufferedReader br =NULL;7FileInputStream FIS =NULL;8FileOutputStream fos =NULL;9         Try {TenFIS =NewFileInputStream (SRC); OneFOS =NewFileOutputStream (dest); AInputStreamReader IR =NewInputStreamReader (FIS, "GBK"); -OutputStreamWriter ow =NewOutputStreamWriter (FOS, "GBK"); -BW =NewBufferedWriter (OW); theBR =NewBufferedReader (IR); - String str; -              while(str = br.readline ())! =NULL) { - bw.write (str); + bw.newline (); - Bw.flush (); +             } A}Catch(IOException e) { at e.printstacktrace (); -         } -         Try { - bw.close (); - br.close (); -}Catch(IOException e) { in e.printstacktrace (); -         } to     } +}

Summary of Io flows in Java

Related Article

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.