67,513
社区成员
发帖
与我相关
我的任务
分享
public class DirCopy {
private static final Logger logger = Logger.getLogger(DirCopy.class);
public static boolean copy(String file1, String file2) {
File in = new File(file1);
File out = new File(file2);
if (!in.exists()) {
logger.error(in.getAbsolutePath() + "源文件路径错误!!!");
return false;
} else {
logger.debug("源文件路径" + in.getAbsolutePath());
logger.debug("目标路径" + out.getAbsolutePath());
}
if (!out.exists())
out.mkdirs();
File[] file = in.listFiles();
FileInputStream fin = null;
FileOutputStream fout = null;
for (int i = 0; i < file.length; i++) {
if (file[i].isFile()) {
try {
fin = new FileInputStream(file[i]);
} catch (FileNotFoundException e) {
logger.error("[execute()]catch:" + e.getMessage());
}
System.out.println("in.name=" + file[i].getName());
try {
fout = new FileOutputStream(new File(file2 + AppConfig.get("jr.dir.FileSeparator")
+ file[i].getName()));
} catch (FileNotFoundException e) {
logger.error("[execute()]catch:" + e.getMessage());
}
//System.out.println(file2);
int c;
byte[] b = new byte[1024 * 5];
try {
while ((c = fin.read(b)) != -1) {
fout.write(b, 0, c);
System.out.println("复制文件中!");
}
fin.close();
fout.flush();
fout.close();
} catch (IOException e) {
logger.error("[execute()]catch:" + e.getMessage());
}
} else
copy(file1 + AppConfig.get("jr.dir.FileSeparator") + file[i].getName(), file2 + AppConfig.get("jr.dir.FileSeparator")
+ file[i].getName());
}
return true;
}
public static void deleteDir(File dir) {
if (dir == null || !dir.exists() || !dir.isDirectory())
return; // 检查参数
for (File file : dir.listFiles()) {
if (file.isFile())
file.delete(); // 删除所有文件
else if (file.isDirectory())
deleteDir(file); // 递规的方式删除文件夹
}
dir.delete();// 删除目录本身
}
public static void main(String[] args)
{
deleteFile(new File("e:\\"));
}
public static void deleteFile(File file)
{
if(file.isFile())
{
file.delete();
}
else
{
File[] files = file.listFiles();
for(File itemFile:files)
{
deleteFile(itemFile);
}
file.delete();
}
}