Java-IO:复制文件

时间:2018-12-10
本文章向大家介绍Java-IO:复制文件,主要包括Java-IO:复制文件相关应用实例、知识点总结和注意事项,具有一定的参考价值,需要的朋友可以参考一下。
 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 }