PackageCom.io;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;Importorg.junit.Test;/** * * @authorAdministrator * 1, Stream classification * (1) According to the different data flow, divided into the input stream and output stream * (2) According to the processing data units, divided into byte stream and character stream * (3) According to the role of different, divided into node stream (directly acting on the file, All InputStream outputstream with file), processing flow (for node flow above, increase efficiency) * 2, IO system * Abstract base class node stream (i.e. file stream) slow total flow (one of the processing flows) *in Putstream fileinputstream bufferedinputstream *outputstream fileoutputstream bufferedoutputstream *Read Er filereader bufferedreader *writer FileWriter bufferedwriter * 3, all processing flows are used for the above Four node streams for file manipulation*/ Public classFileinputourputstreamtest {/*** read one byte at a time*/@Test Public voidFileInputStreamTest1 () {FileInputStream fis=NULL; //1. Create a File object first Try{//There are exceptions to capture here, because FIS must close after useFile file1 =NewFile ("Hello1.txt"); //2. Create a FileInputStream objectFIS =NewFileInputStream (FILE1); /**int b = Fis.read ();//read method, read one byte at a time, read to the end of the file return-1 while (b! =-1) {//System.out.pri Ntln (b);//Because the read is byte, here with int receive, so print out is Unicode code, to display letters, plus char forced conversion System.out.println ((char) b); b = Fis.read (); }**/ //The above code can be abbreviated as intb; while((b = Fis.read ())! =-1) {System.out.print (Char) b); } } Catch(IOException e) {e.printstacktrace (); }finally{ Try{fis.close (); } Catch(IOException e) {e.printstacktrace (); } } } /*** Read an array-length byte at a time*/@Test Public voidFileInputStreamTest2 () {File file=NewFile ("Hello1.txt"); FileInputStream FIS=NULL; Try{FIS=Newfileinputstream (file); byte[] B =New byte[5]; intLen//Len is the number of bytes that are put into the array each time it is read, and when the end of the file returns-1, the first few Len returns the length of the array, and the last returned len<= array lengthStringBuffer strtmp =NewStringBuffer (""); while(len = Fis.read (b))! =-1) {System.out.println (len); String Str=NewString (b, 0, Len);//note Here The three parameters of the new string, the first is the array, the second is the beginning of the array from the subscript to read, the last one, is the length of the last arraySystem.out.println (str); Strtmp.append (str); } System.out.println ("strtmp-===" +strtmp); }Catch(Exception e) {e.printstacktrace (); }finally{ Try{fis.close (); } Catch(IOException e) {e.printstacktrace (); } }} @Test Public voidfileoutputstreamtest () {File file=NewFile ("Hello_out.txt"); FileOutputStream Fos=NULL; Try{fos=Newfileoutputstream (file); byte[] strtmp =NewString ("I Love China"). GetBytes (); Fos.write (strtmp); }Catch(Exception e) {e.printstacktrace (); }finally{ Try{fos.close (); } Catch(IOException e) {e.printstacktrace (); } } } /*** Read content from one file, write to another file * that is, copy the file*/@Test Public voidfileinputoutputstreamtest () {FileInputStream fis=NULL; FileOutputStream Fos=NULL; File Filein=NewFile ("Hello1.txt"); File Fileout=NewFile ("Hello2.txt"); Try{FIS=NewFileInputStream (Filein); FOS=NewFileOutputStream (fileout); intLen; byte[] B =New byte[20];//set the size of the array according to the file size while(len = Fis.read (b))! =-1) {Fos.write (b,0, Len);//0 is starting from the No. 0 bit of byte array b } }Catch(Exception e) {e.printstacktrace (); }finally{ Try{fis.close (); } Catch(IOException e) {e.printstacktrace (); } Try{fos.close (); } Catch(IOException e) {e.printstacktrace (); } } }}
2-byte stream