1 PackageCom.io;2 ImportJava.io.File;3 ImportJava.io.FileInputStream;4 ImportJava.io.FileOutputStream;5 Importjava.io.FileNotFoundException;6 Importjava.io.IOException;7 Public classTest2 {8 9 /**Ten * @paramargs One * @throwsIOException A */ - Public Static voidMain (string[] args) { - theFile file1 =NewFile ("C:\\users\\coda\\desktop\\google1.jpg"); -File file2 =NewFile ("c:\\users\\coda\\desktop\\ Picture storage \\dd.jpg"); - - Copyit (file1,file2); + - } + A Public Static voidcopyit (file file1, file file2) { at - FileInputStream FileInputStream; - Try { -FileInputStream =NewFileInputStream (file1); -}Catch(FileNotFoundException e) { -System.out.println ("File path Error"); in Throw NewRuntimeException (e); - } toFileOutputStream FileOutputStream =NULL; + Try { -FileOutputStream =NewFileOutputStream (File2,true); the}Catch(FileNotFoundException e) { * //TODO auto-generated Catch block $ e.printstacktrace ();Panax Notoginseng } - the byte[]B =New byte[2]; + intCount = 0; A Try { the while(count = Fileinputstream.read (b))!=-1){ + - Fileoutputstream.write (b); $ $ } -}Catch(IOException e) { -System.out.println ("Write file Failed"); the Throw NewRuntimeException (e); - Wuyi}finally{ the Try { - fileoutputstream.close (); Wu}Catch(IOException e) { -System.out.println ("The file is running and cannot be closed"); About Throw NewRuntimeException (e); $ } - Try { - fileinputstream.close (); -}Catch(IOException e) { ASystem.out.println ("The file is running and cannot be closed"); + Throw NewRuntimeException (e); the } -System.out.println ("File copy succeeded"); $ } the the } the the}
Read and write operations for Java IO stream---files