import java
.io
.File
;
import java
.io
.FileInputStream
;
import java
.io
.FileOutputStream
;
import java
.io
.IOException
;
public class MyTest2 {
public static void main(String
[] args
) {
copyDir("D:\\Documents\\Downloads\\jieya", "D:\\");
}
public static void copyDir(String fileAdress
, String copyString
) {
File file
= new File(fileAdress
);
if (file
.isFile()) {
try {
copyFile(file
.getAbsolutePath(), copyString
+"\\"+file
.getName());
} catch (IOException e
) {
e
.printStackTrace();
}
} else {
File file1
= new File(copyString
+"\\"+file
.getName());
file1
.mkdirs();
String
[] list
= file
.list();
for (int i
= 0; i
< list
.length
; i
++) {
copyDir(file
.getAbsolutePath() + "\\" + list
[i
], file1
.getAbsolutePath() );
}
}
}
public static void copyFile(String oldString
, String copyString
) throws IOException
{
FileInputStream in
= new FileInputStream(oldString
);
FileOutputStream out
= new FileOutputStream(copyString
);
int len
= 0;
byte[] bytes
= new byte[1024 * 8];
while ((len
= in
.read(bytes
)) != -1) {
out
.write(len
);
}
in
.close();
out
.close();
}
}
转载请注明原文地址: https://mac.8miu.com/read-490984.html