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("请输入正确的文件名或路径名");
}
}
}
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("请输入正确的文件名或路径名");
}
}
}
相关文章推荐
- RxJava变换操作符:.concatMap( )与.flatMap( )的比较
- 修改Eclipse中web项目名称
- JAVA学习笔记(3-2-函数与重载)
- 单例模式的反序列化总结
- Java随笔--分布式
- maven+myEclipse+ssm的搭建
- Java什么时候提高境界支持async/await写法啊?
- 一些Spring MVC的使用技巧
- 服务器环境之2:ubuntu14.04安装jdk1.7
- 服务器环境之3:ubuntu14.04安装maven3.3.9
- Java 泛型简写
- Arrays.asList的实现
- java内存区域及每个区域的简介
- java虚拟机在java堆中对象内存分配、布局、访问
- OutOfMemoryError异常 jdk1.8
- Spring 4.3 的新功能和增强
- Java知识图谱收集整理
- Java 下的 JSON库性能比较:JSON.simple vs. GSON vs. Jackson
- java web项目中使用Socket通信多线程、长连接
- Java 类加载机制详解