您的位置:首页 > 编程语言 > Java开发

Java批量文件夹下所有文件,并且修改文件后缀名

2016-07-01 00:00 316 查看
摘要: 通过流技术将D:\images目录下的所有图片复制到F:\images目录下,同时将这些图片名字修改为:原来名称_副本.后缀

import java.io.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyAllIma {
// 通过流技术将D:\images目录下的所有图片复制到F:\images目录下,同时将这些图片名字修改为:原来名称_副本.后缀

public static void main(String[] args) {
try {
copyAll("d:/images", "E:/images");
} catch (IOException e) {
e.printStackTrace();
}

}

public static void copyAll(String path, String copyPath) throws IOException {
File filePath = new File(path);
DataInputStream read;
DataOutputStream write;
// 判断filePath是否是文件夹
if (filePath.isDirectory()) {
// 如果是,找出所有filePath路径下的文件,放到File数组中
File[] list = filePath.listFiles();
// 遍历File数组
for (int i = 0; i < list.length; i++) {
String newPath = path + File.separator + list[i].getName();
String str=list[i].getName();
String str1=str.substring(str.indexOf("."));
String str2=str.substring(0,str.indexOf("."));
str=str2+"_副本"+str1;
String newCopyPath = copyPath + File.separator + str;
// 递归调用,保证能够找到所有资源
copyAll(newPath, newCopyPath);

}
} else if (filePath.isFile()) {
read = new DataInputStream(new BufferedInputStream(new FileInputStream(path)));
write = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(copyPath)));
byte[] buf = new byte[1024 * 512];
while (read.read(buf) != -1) {
write.write(buf);
}
&nbs
7fe0
p; read.close();
write.close();
} else {
System.out.println("请输入正确的文件名或路径名");
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: