已知源目录路径sourceFilePath,此目录下还有多级子目录和多个文本文件(*.txt)。尝试编写一个方法,将此目录下所有的文件拷贝至另一个目录targetFilePath,并其中的文本文件修改成SQL文件(*.SQL)。
2024-08-30 23:47:03
public void copyFile(String oldPath, String newPath) throws IOException {
(new File(newPath)).mkdirs();
String[] file = new File(oldPath).list();
File fileTemp = null;
String separator = File.separator;
for (int i = 0; i < file.length; i++) {
if (oldPath.endsWith(separator)) {
fileTemp = new File(oldPath + file[i]);
} else {
fileTemp = new File(oldPath + separator + file[i]);
}
if (fileTemp.isFile()) {
FileInputStream fis = new FileInputStream(fileTemp);
if (file[i].endsWith(".txt")) {
fileTemp = new File(oldPath + separator + file[i].substring(0, file[i].length() - 4) + ".sql");
}
FileOutputStream fos = new FileOutputStream(newPath + "/" + fileTemp.getName());
byte[] b = new byte[1024 * 5];
int len;
while ((len = fis.read(b)) != -1) {
fos.write(b, 0, len);
}
fos.flush();
fos.close();
fis.close();
}
if (fileTemp.isDirectory()) {
this.copyFile(oldPath + "/" + file[i], newPath + "/" + file[i]);
}
}
}
最新文章
- My year of 2016
- springboot+dubbo
- 按钮点击,打开qq,用做联系客服
- php 升级到 5.3+ 后出现的一些错误,如 ereg(); ereg_replace(); 函数报错
- Extjs TabPanel 选项卡延迟加载
- Installing MySQL Server on CentOS
- Hibernate缓存之初探
- Form_Form Builder国际化多语言开发(案例)
- 在Windows下Mysql如何重置root用户密码
- z-index无效
- iOS开发那些事--性能优化–内存泄露问题的解决(转)
- dagger和butterknife使用冲突
- 【解决】Microsoft Visual Studio 2012 打开2008下编译的silverlight3项目
- 使用socket.io打造公共聊天室
- 数据挖掘相关的10个问题[ZZ]
- vue实现仿淘宝结账页面
- Python函数篇:装饰器
- HTTP常用方法
- nginx.conf添加lua.conf配置
- Win7环境 搭建IIS环境。发布asp.net MVC项目到IIS(第一期)