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