Java-IO:複製檔案

JserSuper發表於2018-12-10
 1 import java.io.FileInputStream;
 2 import java.io.FileNotFoundException;
 3 import java.io.FileOutputStream;
 4 import java.io.IOException;
 5 
 6 public class CopyFileDemo {
 7     public static void main(String[] args) {
 8 
 9         FileInputStream fis = null;
10         FileOutputStream fos = null;
11         try {
12             fis = new FileInputStream("js.mp3");
13             fos = new FileOutputStream("copy_js.mp3");
14             // BufferedInputStream bis=new BufferedInputStream(fis);
15             // BufferedOutputStream bos=new BufferedOutputStream(fos);
16             byte[] bt = new byte[1024];
17             int len = 0;
18             while ((len = fis.read(bt)) != -1) {
19                 fos.write(bt, 0, len);
20             }
21         } catch (FileNotFoundException e) {
22             e.printStackTrace();
23         } catch (IOException e) {
24             e.printStackTrace();
25         } finally {
26             try {
27                 if (fis != null)
28                     fis.close();
29                 if (fos != null)
30                     fos.close();
31             } catch (IOException e) {
32                 e.printStackTrace();
33             }
34         }
35     }
36 }

 

相關文章