package com.zz.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileCopyAndCut {
public static void main (String[] args) throws IOException{
// checkFiles("E:\\1");
CopyDir("E:\\1","E:\\2");
// CutFile("Z:\\1","Z:\\2");
}
public static void CopyDir(String filePath1,String filePath2) throws IOException{
File f1 = new File(filePath1);
File f2 = new File(filePath2);
File[] fs = f1.listFiles();
if(!f2.exists()) {
f2.mkdir();
}
for(File temp:fs) {
if(temp.isDirectory()) {
CopyDir(filePath1+File.separator+temp.getName(),filePath2+File.separator+temp.getName());
}
if(temp.isFile()) {
CopyFile(filePath1+File.separator+temp.getName(),filePath2+File.separator+temp.getName());
}
}
}
public static void CopyFile(String filePath1,String filePath2) throws IOException{
File oldFile = new File(filePath1);
File newFile = new File(filePath2);
FileInputStream in = new FileInputStream(oldFile);
FileOutputStream out = new FileOutputStream(newFile);
byte[] b = new byte[(int)oldFile.length()];
in.read(b);
in.close();
out.write(b);
out.close();
}
public static void CutFile(String filePath1,String filePath2) throws IOException{
File f1 = new File(filePath1);
CopyDir(filePath1,filePath2);
getDelete(f1);
}
public static void getDelete(File f) {
File[] fs = f.listFiles();
if(fs!=null) {
for(File f2:fs) {
if(f2.isFile()) {
f2.delete();
}else if(f2.isDirectory()) {
getDelete(f2);
f2.delete();
}
}
}
}
}
复制文件中暂时只包含txt, 其中内容一并复制
效果图
复制
剪切