Copy folder

From , 5 Years ago, written in Java, viewed 222 times.
URL https://pastebin.vip/view/cb8acb1d
  1. package cn.itcast_05;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.BufferedOutputStream;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9.  
  10. /*
  11.  * 复制文件夹:
  12.  *
  13.  * 数据源:e:\\demo
  14.  *  目的:e:\\test
  15.  *
  16.  */
  17.  
  18. public class CopyFolderDemo {
  19.         public static void main(String[] args) throws IOException {
  20.                 //源文件夹
  21.                 File srcFolder = new File("f:\\demo");
  22.                 //目的文件夹
  23.                 File destFolder = new File("f:\\test");
  24.                 if(!destFolder.exists()){
  25.                         destFolder.mkdirs();
  26.                 }
  27.                
  28.                 File[] fileArray = srcFolder.listFiles();
  29.                
  30.                 for(File file:fileArray){
  31.                         //拼接新的文件名和路径
  32.                         File newFile = new File(destFolder,file.getName());
  33.                         System.out.println(newFile);
  34.                        
  35.                         //调用方法进行复制
  36.                         CopyFile(file,newFile);
  37.                        
  38.                        
  39.                        
  40.                 }      
  41.                
  42.         }
  43.  
  44.         private static void CopyFile(File file, File newFile) throws IOException {
  45.                 //创建源对象
  46.                 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
  47.                 //创建目的对象
  48.                 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));
  49.                
  50.                 //读写数据
  51.                 byte [] bt = new byte[1024];
  52.                 int len= 0 ;
  53.                 while((len=bis.read(bt))!=-1){
  54.                         bos.write(bt, 0, len);
  55.                 }
  56.                
  57.                 //释放资源
  58.                 bis.close();
  59.                 bos.close();
  60.                
  61.                
  62.         }
  63.  
  64. }
  65.  

Reply to "Copy folder"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website