Javaによる画像など他のファイルのローカルコピー

6472 ワード

くだらない話は多くなくて直接1段の簡単な教程のコードに行って、自分で下りて行ってみて、多くたたいて多く練習します!!
このコードは、画像を「Fディスク」の下から「Eディスク」にコピーする新しいパスです.
 1 package SchoolHome_Four;
 2 
 3 import java.io.DataInputStream;
 4 import java.io.DataOutputStream;
 5 import java.io.FileInputStream;
 6 import java.io.FileOutputStream;
 7 import java.io.IOException;
 8 
 9 public class Demo {
10 
11     public static void main(String[] args) {
12         String oldSrc = "F:\\  \\ .jpg";                                                //      
13         String newSrc = "E:\\javaDoc\\ .jpg";                                            //      
14         try {
15             copyPicture(oldSrc,newSrc);
16         } catch(Exception e) {
17             e.printStackTrace();
18         }
19     }
20     
21     public static void copyPicture(String oldSrc,String newSrc) throws IOException {
22         //        
23         FileInputStream fis = new FileInputStream(oldSrc);                            //     ,          
24         DataInputStream dis = new DataInputStream(fis);                                //        ,          
25         FileOutputStream fos = new FileOutputStream(newSrc);                        //     ,          
26         DataOutputStream dos = new DataOutputStream(fos);                            //        ,          
27         byte[] b = new byte[1024];                                                        //       
28         int length = -1;                                                                    //           
29         //    
30         while((length = dis.read(b)) != -1) {                                                //  read(byte[] b)                       
31             dos.write(b,0,length);                                                            // byte              
32         }
33         //     
34         dis.close();
35         dos.flush();
36         fis.close();
37         fos.close();
38         System.out.println("      !");
39     }
40 }