java 复制文件

 1  public void copyFile(String oldPath, String newPath) { 
 2         try { 
 3         int bytesum = 0; 
 4         int byteread = 0; 
 5         File oldfile = new File(oldPath); 
 6         File newfile = new File(newPath);
 7         if(newfile.exists()) {
 8             newfile.mkdirs();
 9         }
10         if (oldfile.exists()) { //文件存在时 
11         InputStream inStream = new FileInputStream(oldPath); //读入原文件 
12         FileOutputStream fs = new FileOutputStream(newPath); 
13         byte[] buffer = new byte[1024]; 
14         int length; 
15         while ( (byteread = inStream.read(buffer)) != -1) { 
16         bytesum += byteread; //字节数 文件大小 
17         System.out.println(bytesum); 
18         fs.write(buffer, 0, byteread); 
19         } 
20         inStream.close(); 
21         } 
22         } 
23         catch (Exception e) { 
24         System.err.println("复制单个文件操作出错"); 
25         e.printStackTrace(); 
26         } 
27         } 
28     

注意:path为物理路径,就是D:/aaa/bbb

posted @ 2019-04-23 18:11  当敌人找上门来  阅读(93)  评论(0)    收藏  举报