Section 24th (Java file stream, buffered stream)

Source: Internet
Author: User
Tags ming

Java stream files are usually composed of a series of bytes or characters, the sequence of bytes that make up a file is called a byte stream, and the sequence of characters that make up a file is called a character stream. In Java, the direction of the stream can be divided into input and output streams. The input stream is the process of loading data from a file or other input device into memory; the output stream is the opposite of saving the in-memory data to a file or other output device. See figure: The file is composed of characters or bytes, then load the file into memory or output file to a file, need to have input and output stream support, then in the Java language, the input and output stream is divided into two, byte input and output stream, character input and output stream InputStream is a byte input stream: InputStream is an abstract class, and all classes that inherit InputStream are byte input stream outputstream (byte output stream): Inherited OutputStream are byte output stream reader (character input stream): inherited Reader is a character flow writer (character output stream): Inherited writer is a character output stream
File streams are mainly divided into: file byte input stream, file byte output stream, file character input stream, file character output stream FileInputStream (file byte input stream): Read the file fileoutputstream (file byte output stream) mainly in byte mode: Writes the file FileReader (file character input stream) primarily in bytes: FileReader is a single-character reading of a file, that is, reading two bytes at a time filewriter (file character output stream)
/*java.io.InputStream byte input stream java.io.FileInputStream file byte input stream reads files in byte mode*/import java.io.*; Public classfileinputstreamtest01{ Public Static voidMain (string[] args) {FileInputStream fis=NULL; Try{FIS=NewFileInputStream ("C:\\work\\java\\course\\vip\\javase-course-arry\\chapter25\\test01.java"); inti = Fis.read ();//read in bytesSystem. out. println ("i ="+ i);//read out as-1                }Catch(FileNotFoundException e) {e.printstacktrace (); }Catch(IOException e) {e.printstacktrace (); } finally{            //close File input stream            if(FIS! =NULL){                Try{fis.close (); }Catch(IOException e) {e.printstacktrace (); }            }                }    }}
/*int read (byte[] b); Reads a maximum of b.length bytes of data from this input stream into a byte array. */import java.io.*; Public classfileinputstreamtest02{ Public Static voidMain (string[] args) {Try{FileInputStream fis=NewFileInputStream ("C:\\work\\java\\course\\vip\\javase-course-arry\\chapter25\\test01.java"); //prepare a byte array            byte[] bytes =New byte[3];//read up to three bytes per time//int read (byte[] b);            intI1 =fis.read (bytes); System. out. println ("i ="+i1); System. out. println (NewString (bytes));//Output garbled            /*garbled, file normal read, but our characters garbled, because the use of a byte input stream, he is a byte read, so read a byte to print, then the Chinese character is not complete, so it is garbled */                    } Catch(Exception e) {e.printstacktrace (); }    }}
/*Backup of files (copy and paste)*/import java.io.*; Public classfileoutputstreamtest01{ Public Static voidMain (string[] args) {FileInputStream fis=NULL; FileOutputStream Fos=NULL; Try{FIS=NewFileInputStream ("c:\\work\\keke.jpg"); FOS=NewFileOutputStream ("c:\\work\\java\\keke.jpg"); System. out. println ("file copy is starting ....."); //while reading, writing on the side            inttemp =0;//Temp Variable             while(temp = Fis.read ())! =-1) {fos.write (temp); } System. out. println ("File copy complete! "); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } finally{            if(FIS! =NULL){                Try{fis.close (); } Catch(Exception e) {e.printstacktrace (); }            }            if(Fos! =NULL){                Try{fos.close (); } Catch(Exception e) {e.printstacktrace (); }                            }        }    }}
/*the character input stream of the FileReader file reads the file in characters, that is, reads two bytes at a time*/import java.io.*; Public classfilereadertest01{ Public Static voidMain (string[] args) {FileReader fr=NULL; Try{FR=NewFileReader ("C:\\work\\ming.txt"); inttemp =0;  while(temp = Fr.read ())! =-1){                //Output CharacterSystem. out. println (temp); }                        //why not garbled? Because a character input stream is used to read a text file//so Chinese characters are not garbled (read two bytes at a time < that is, a character >)                    } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } finally{            Try{                if(FR! =NULL) {fr.close (); }            } Catch(IOException e) {e.printstacktrace (); }        }        }    }
/*FileWriter file character output stream Java.lang.Object inheritors java.io.Writer inheritors java.io.OutputStreamWriter inheritors java.io.Fi Lewriter*/import java.io.*; Public classfilewritertest01{ Public Static voidMain (string[] args) {FileWriter fw=NULL; Try{            //true to append after fileFW =NewFileWriter ("C:\\work\\ming.txt",true); //line break (\ r \ n)Fw.write ("\r\n\r\n is great! Zhongshan College"); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } finally{            Try{                if(FW! =NULL) {fw.close (); }            } Catch(IOException e) {e.printstacktrace (); }        }                    }}
/*copy code with byte buffer stream transformation file*/import java.io.*; Public classbufferedstreamtest01{ Public Static voidMain (string[] args) {Bufferedinputstream bis=NULL; Bufferedoutputstream Bos=NULL; Try{                        //FileInputStream fis = new FileInputStream ("C:\\work\\arry.txt");bis=NewBufferedinputstream (NewFileInputStream ("c:\\work\\keke.jpg")); //FileOutputStream fos = new FileOutputStream ("C:\\work\\java\\course\\arry.txt.bak");BOS =NewBufferedoutputstream (NewFileOutputStream ("c:\\work\\java\\course\\keke.jpg")); System. out. println ("file copy is starting ....."); inttemp =0;  while(temp = Bis.read ())! =-1) {bos.write (temp); }                        //RefreshBos.flush (); System. out. println ("file copy succeeded! "); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } finally{            Try{                if(bis! =NULL) {bis.close (); }                if(Bos! =NULL) {bos.close (); }            } Catch(IOException e) {e.printstacktrace (); }        }            }}

Section 24th (Java file stream, buffered stream)

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.