package com.zhang.IODemo;
import java.io.*;
public class CutFile {
public static void main(String[] args) {
File fatherFile = new File("D:\\CopyTest");
File sonFile = new File("D:\\CutFile");
directoryTodirectory(fatherFile, sonFile);
}
//文件夹到文件夹
public static void directoryTodirectory(File fatherFile, File sonFile) {
File makeFile = new File(sonFile, fatherFile.getName());
if (!makeFile.exists()) {
makeFile.mkdirs();
}
File[] fatherPath = fatherFile.listFiles();
if (fatherPath == null) {
return;
}
for (File file : fatherPath) {
if (file.isDirectory()) {
directoryTodirectory(file, makeFile);
}
if (file.isFile()) {
pathToPath(file, makeFile);
file.delete();
}
}
fatherFile.delete();
}
//文件到文件
public static void pathToPath(File fatherFile, File sonFile) {
File outPutFile = new File(sonFile, fatherFile.getName());
try (
InputStream bis = new BufferedInputStream(new FileInputStream(fatherFile));
OutputStream bos = new BufferedOutputStream(new FileOutputStream(outPutFile));
) {
byte[] buffer = new byte[1024];
int len;
while ((len = bis.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
}
01-07
5770
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-30
1611
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-02
2899
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交