public static void copeDirAndFile(String sourcePath,int subLength,String targetPath) throws Exception{
//判断你输入的路径是否是一个非法的路径
// if(path == null){
//
// }
//创建原来的文件
File file = new File(sourcePath);
//判断"文件"是否是一个目录,如果原来的文件是一个文件夹,则新创建的文件也是一个文件夹
if(file.isDirectory()){
String fopath = sourcePath.substring(subLength);
File fo = new File(targetPath + fopath);
fo.mkdir();
File[] files = file.listFiles();
for(File f : files){
copeDirAndFile(f.getPath(),subLength,targetPath);
}
}else{
FileInputStream fis = new FileInputStream(file);
//截取文件的路径,除去根路径
File fo = new File(targetPath + sourcePath.substring(subLength));
//判断路径中有没有点,如果有点,则是一个文件夹,如果没有点,则是一个文件。
FileOutputStream fos = new FileOutputStream(fo);
byte[] by = new byte[1024];
int temp = 0;
while((temp=fis.read(by))>-1){
fos.write(by, 0, temp);
}
fos.close();
fis.close();
}
}
public static void main(String[] args) throws Exception {
String sourcePath = "d:\\cpath\\";
String targetPath = "e:\\cpath\\";
copeDirAndFile(sourcePath,9,targetPath);
System.out.println("ok");
}
复制文件夹里的文件到另外一个地方
最新推荐文章于 2022-07-07 11:25:07 发布