这是最近学到的几个关于IO文件操作的几个小算法,今天总结出来。


1. 删除一个给定的目录,这上目录不为空目录,使用递归来实现

public void test04(File file) {
File[] listFiles = file.listFiles();
for (File f : listFiles) {
if (f.isFile()) {// 是文件
f.delete();
} else {// 是文件夹
test04(f);
f.delete();
}
}
file.delete();
}

2. 写一个方法,可以复制一个目录,(此目录不为空)

public void testCopyFolder() throws IOException {
File file = new File("e:/java");
File copyFile = new File("e:/copyjava");
copyFolder(file,copyFile);
} public void copyFolder(File src, File dest) throws IOException {
// 文件夹的处理
if (src.isDirectory()) {
if (!dest.exists()) {
dest.mkdir();
} String files[] = src.list();
for (String file : files) {
File srcFile = new File(src, file);
File descFile = new File(dest, file);
copyFolder(srcFile, descFile);// 递归调用
}
} else {// 文件的复制
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest); byte[] bt = new byte[1024];
int len = 0;
while ((len = in.read(bt)) > 0) {
out.write(bt, 0, len);
}
out.close();
in.close();
}
}

最新文章

  1. 1Z0-053 争议题目解析704
  2. JPA 2.1 Coverter 注解
  3. Spark之SQL解析(源码阅读十)
  4. Visual Studio 2010安装教程
  5. Linux驱动框架之framebuffer驱动框架
  6. c++中__declspec用法总结
  7. 常用ASP函数的封装
  8. js--事件对象的理解4
  9. Automatic Preferred Max Layout Width is not available on iOS versions prior to
  10. nova创建虚拟机源码分析系列之四 nova代码模拟
  11. 《程序设计入门——C语言》翁恺老师 第二周编程练习记录
  12. 【Go】优雅的读取http请求或响应的数据-续
  13. Apollo 1 融合 Spring 的三个入口
  14. websocket 工作原理
  15. SQL Server 查询中文字段返回为空
  16. k-近邻算法(KNN)
  17. 创建自己的共用js库
  18. SSM整合配置文件的主要内容
  19. Mybatis -- 批量更新 -- updateBatch
  20. shell_exec

热门文章

  1. 转:关于PHP性能优化
  2. hbase 0.96 单机伪分布式配置文件及遇到的问题 find命令
  3. Problem A: The Monocycle
  4. Delphi中ADO异步执行方式
  5. RTF格式文件浅析
  6. fedora下体验gentoo安装
  7. 几种任务调度的 Java 实现方法与比较Timer,ScheduledExecutor,Quartz,JCronTab
  8. Final对象
  9. SRM 507(2-1000pt)
  10. docs