用java实现整个文件夹的复制

页面导航:首页 > 软件编程 > Java编程 > 用java实现整个文件夹的复制

用java实现整个文件夹的复制

来源: 作者: 时间:2016-01-21 09:39 【

package copy; import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.File;import java.io.FileI...
package copy;
 
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyFile {
 public static void main(String[] args) throws IOException {
  String path = "E://test";
  String copyPath = "F://TEST";
  copy(path,copyPath);
  SaveName Change=new SaveName();
  Change.main(args);
 }
 

 public static void copy(String path, String copyPath) throws IOException{
  File filePath = new File(path);
  DataInputStream read ;
  DataOutputStream write;
  if(filePath.isDirectory()){
   File[] list = filePath.listFiles();
   for(int i=0; i<list.length; i++){
    String newPath = path + File.separator + list[i].getName();
    String newCopyPath = copyPath + File.separator + list[i].getName();
    File newFile = new File(copyPath);
    if(!newFile.exists()){
     newFile.mkdir();
    }
    copy(newPath, newCopyPath);
   }
  }else if(filePath.isFile()){
   read = new DataInputStream(
     new BufferedInputStream(new FileInputStream(path)));
   write = new DataOutputStream(
     new BufferedOutputStream(new FileOutputStream(copyPath)));
   byte [] buf = new byte[1024*512];
   while(read.read(buf) != -1){
    write.write(buf);
   }
   read.close();
   write.close();
  }else{
   System.out.println("请输入正确的文件名或路径名");
  }
 }
 
}

 

Tags:

相关文章

    文章评论

    最 近 更 新
    热 点 排 行
    Js与CSS工具
    代码转换工具
    
    <