【#文档大全网# 导语】以下是®文档大全网的小编为您整理的《文件及文件及的复制java代码》,欢迎阅读!
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException;
public class CTest { /** * @param args */ public static void main(String[] args) { String filePath = "G:/12/path"; String copypath="G:/12/copypath"; File file = new File(filePath); if(!file.exists()){ System.out.println("文件不存在"); } if(!new File(copypath).exists()){ new File(copypath).mkdirs(); System.out.println("文件以创建"); } //获取文件下的文件或目录 File[] filelist = file.listFiles(); for(int i=0;i if(filelist[i].isFile()){ copyFile(filelist[i], copypath+file.separator+filelist[i].getName()); }else{ String target=copypath+file.separator+filelist[i].getName(); copyDirecty(filelist[i],target); } } } /** * 复制文件夹 * @param filelist * @param copypath */ private static void copyDirecty(File filelist, String path) {
}
}
(new File(path)).mkdirs();
//判断源文件目录下的文件/文件夹 File[] list = filelist.listFiles(); for(int i=0;i if(list[i].isFile()){ copyFile(list[i], path+"/"+list[i].getName()); }else{ //目标文件目录 String target=path+"/"+list[i].getName(); copyDirecty(list[i],target); } }
/**
* 复制文件 * @param filelist * @param copypath */
private static void copyFile(File filelist, String copypath) { try { FileInputStream in=new FileInputStream(filelist); FileOutputStream out=new FileOutputStream(copypath); BufferedInputStream bin=new BufferedInputStream(in); BufferedOutputStream bout=new BufferedOutputStream(out); int len=0; byte[] b = new byte[8*1024]; while(bin.read(b)!=-1){ bout.write(b, 0, len); } bout.flush(); bout.close(); bin.close(); out.close(); in.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
本文来源:https://www.wddqxz.cn/42f19418650e52ea551898b2.html