Objective:
Java streaming is an important content, the basic file read and write and copy knowledge point is a lot of interviews. Therefore, this paper makes a simple test summary.
2. Figure shows "text io/binary io"(this is a summary of the reference from the Web, quite classic, easy to compare memory)
3. Text copy of the experimental Java Implementation code:
1 PackageCom.gdufe.io;2 3 ImportJava.io.BufferedInputStream;4 ImportJava.io.BufferedOutputStream;5 ImportJava.io.DataInputStream;6 ImportJava.io.DataOutputStream;7 ImportJava.io.FileInputStream;8 ImportJava.io.FileOutputStream;9 Importjava.io.IOException;Ten ImportJava.io.InputStream; One ImportJava.io.OutputStream; A - Public classTestinputoutputstream { - the //private final static String source= "T.txt"; - //private final static String target= "Tt.txt"; - //private final static String source= "P.png"; - //private final static String target= "Pp.png"; + Private Final StaticString source= "D:\\tool_software\\mysql-installer-community-5.6.23.0.msi"; - Private Final StaticString target= "Mysql-installer-community-5.6.23.0.msi"; + //D:\Tool_Software\Ryuyan.zip A at - Public Static voidMain (string[] args) { - //testinputoutputstream.copy1 (SOURCE, TARGET); - //testinputoutputstream.copy2 (SOURCE, TARGET); - testinputoutputstream.copy3 (SOURCE, TARGET); -System.out.println ("--end---"); in } - to Public Static voidcopy1 (String src,string tar) { + -InputStream input =NULL; theOutputStream output =NULL; * Try{ $input =NewFileInputStream (SRC);Panax NotoginsengOutput =NewFileOutputStream (TAR); - intR; the while((R=input.read ())!=-1){ +Output.write ((byte) r); A } the}Catch(Exception e) { + e.printstacktrace (); -}finally{ $ Try { $ input.close (); - output.close (); -}Catch(IOException e) { the e.printstacktrace (); - }Wuyi } the - } Wu Public Static voidcopy2 (String src,string tar) { -InputStream input =NULL; AboutOutputStream output =NULL; $ Try{ -input =NewFileInputStream (SRC); -Output =NewFileOutputStream (TAR); - intLength=0; A byte[]B =New byte[1024]; + while((Length=input.read (b))!=-1){ theOutput.write (b,0, length); - } $}Catch(Exception e) { the e.printstacktrace (); the}finally{ the Try { the input.close (); - output.close (); in}Catch(IOException e) { the e.printstacktrace (); the } About } the } the the Public Static voidcopy3 (String src,string tar) { +InputStream input =NULL; -OutputStream output =NULL; the Try{Bayiinput =NewDataInputStream (NewBufferedinputstream (NewFileInputStream (SRC))); theOutput =NewDataOutputStream (NewBufferedoutputstream (NewFileOutputStream (TAR))); the /*** After the pro-test, buffer buffered stream reads really faster some * * **/ - - intLength=0; the byte[]B =New byte[1024];//first read the stream into a byte array the while((Length=input.read (b))!=-1){ theOutput.write (b,0, length); the } -}Catch(Exception e) { the e.printstacktrace (); the}finally{ the Try {94 input.close (); the output.close (); the}Catch(IOException e) { the e.printstacktrace ();98 } About } - }101}
(Attached: The reference code when the attention to prepare the test file, otherwise the exception "file can ' t be found"!)
Copy files to the direct directory of the Java Project, refresh project to view!
)
Java implements local FileCopy