java拷贝文件夹和删除文件夹
1.文件夹的拷贝
public void copy(file src, file dest) throws ioexception {
if (dest.isfile()) {
throw new runtimeexception(dest + "不是文件夹");
}
file file = new file(dest.getpath() + "/" + src.getname());
if (src.isfile()) {
fileinputstream fis = new fileinputstream(src);
bufferedinputstream bis = new bufferedinputstream(fis);
fileoutputstream fos = new fileoutputstream(file);
bufferedoutputstream bos = new bufferedoutputstream(fos);
byte[] bytes = new byte[1024];
int len;
while ((len = bis.read(bytes)) != -1) {
bos.write(bytes, 0, len);
}
bis.close();
fis.close();
bos.close();
fos.close();
} else if(src.isdirectory()){
file.mkdir();
file[] files = src.listfiles();
for (file file2 : files) {
copy(file2, dest);
}
}
}
2. 删除整个文件夹:
public void delete(file dir) {
if (dir.isdirectory()) {
file[] files = dir.listfiles();
for (file file : files) {
delete(file);
}
}
dir.delete();
}