标签:while 删除文件 buffere dir out stream close 拷贝文件 cep
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();
}
标签:while 删除文件 buffere dir out stream close 拷贝文件 cep
原文地址:https://www.cnblogs.com/dirsoen/p/12618525.html