FileCopy文件复制

 1 package cn.com.filecopy;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.FileNotFoundException;
 5 import java.io.FileOutputStream;
 6 import java.io.IOException;
 7 import java.util.Scanner;
 8 
 9 public class FileCopy {
10     void copyfile(String src,String dec){
11         FileInputStream fis = null;
12         FileOutputStream fos = null;
13         byte[] buffer = new byte[1024];
14         int temp=0;
15         try {
16             fis = new FileInputStream(src);
17             fos = new FileOutputStream(dec);
18             while((temp=fis.read(buffer))!=-1 ){
19                 fos.write(buffer, 0, temp);
20             }
21         } catch (FileNotFoundException e) {
22             // TODO 自动生成的 catch 块
23             e.printStackTrace();
24         } catch (IOException e) {
25             // TODO 自动生成的 catch 块
26             e.printStackTrace();
27         }finally{
28             try {
29                 fos.close();
30                 fis.close();
31             } catch (IOException e1) {
32                 // TODO 自动生成的 catch 块
33                 e1.printStackTrace();
34             }
35         }
36         
37     }
38     public static void main(String[] args) {
39         Scanner sc = new Scanner(System.in);
40         System.out.println("请输入你copy文件的原始路径:");
41         String src = sc.next();
42         System.out.println("请输入要copy到的目的地(含文件名):");
43         String dec = sc.next();
44         new FileCopy().copyfile(src, dec);
45     }
46 
47 }

一个简单的文件复制代码。如果是剪切,只需复制完成以后删除原文件即可。

原文地址:https://www.cnblogs.com/rememberme/p/FileCopy.html