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(); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自学编程网。
- 本文固定链接: https://zxbcw.cn/post/184092/
- 转载请注明:必须在正文中标注并保留原文链接
- QQ群: PHP高手阵营官方总群(344148542)
- QQ群: Yii2.0开发(304864863)