/ ** * copies of the image to achieve \ * Note: using a byte stream file * / Package com.test4; Import the java.io. * ; public class Demo12_4 { / ** * @param args * / public static void main (String [] args) { // the TODO Auto-Generated Method Stub // Create input stream the FileInputStream FIS = null ; // output stream to create a FileOutputStream fos = null ; the try { // for image may = FIS new new the FileInputStream ( "D: \\ a.jpg" ); fos = new new a FileOutputStream ( "E: \\ a.jpg" ); // for text files may FIS = new new the FileInputStream ( "D: \\ ss.txt " ); fos = new new a FileOutputStream (" E: \\ ss.txt " ); byte buf [] = new new byte [1024 ]; // read cyclically int n-= 0; // record actually read bytes // loop reads the while ((n-fis.read = (buf)) = -!. 1) { // output to the specified file fos.write (buf); System.out.println ( "complete copying files" ); } } the catch (Exception E) { // the TODO Auto-Generated Block the catch e.printStackTrace () ; } the finally { // Close the file stream the try { fis.close (); fos.close (); } the catch (IOException E) { // the TODO Auto Block the catch-Generated e.printStackTrace (); } } } }
Package com.fs.test; / ** * achieve a copy image \ * Note: using a byte stream file * / Import the java.io. * ; public class Test2 { / ** * @param args * / public static void main (String [] args) { // the TODO Auto-Generated Method Stub // Create input stream the FileInputStream FIS = null ; // output stream to create a FileOutputStream fos = null ; the try { //May be used to image FIS = new new the FileInputStream ( "C: \\ 111.jpg" ); fos = new new a FileOutputStream ( "D: \\ 111.jpg" ); byte buf [] = new new byte [1024 ]; // loop reads the while - ((fis.read (buf)) =. 1! ) { // output to the specified file fos.write (buf); System.out.println ( "complete copying files" ); } } the catch ( Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ //关闭文件流 try { fis.close(); fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }