Java实现复制文件并命名的超简洁写法
作者:氢电公敌 时间:2022-02-12 12:54:06
复制文件并命名的超简洁写法
没错又是我,这次为大家带来Java中 复制文件并命名的超简洁写法(请确保你的jre在1.8+),这次用到了Files(始于1.7)和lambda 表达式(始于1.8),都是比较新的东西,同时还有一些振奋人心的特性(和爱)。
好了上代码
DirectoryStream<Path> directoryStream;
File in = new File("C:\\Users\\simon\\Desktop\\a"); // 资源文件夹
File out = new File("C:\\Users\\simon\\Desktop\\b"); // 目标文件夹
try {
directoryStream = Files.newDirectoryStream(in.toPath()); //returning a DirectoryStream to iterate over* all entries in the directory.
directoryStream.forEach(path -> {
if (path.getFileName().toString().endsWith(".java")) { // 判断是否为java文件
try {
Files.copy(path, out.toPath().resolve(path.getFileName().toString().replace(".java", ".txt")), StandardCopyOption.REPLACE_EXISTING); // 重命名为.txt 并且复制到out文件夹
} catch (IOException e) { // 因为在lambda表达式内,所以要包裹try catch
e.printStackTrace();
}
}
});
} catch (IOException e) {
e.printStackTrace();
}
文件重命名拷贝一份新的文件
java文件重命名,并且保留老的文件,实际上就是拷贝一份新的文件,相当于复制粘贴重命名。代码如下:
传参数说明
老的文件地址,oneType和twoType还有count是我自己业务的东西也是文件的重命名名字,files集合是为了方便我把这批文件导出且压缩,参见这篇文章
//对图片进行重命名
public String reNameImg(String oldPath, String oneType, String twoType, int count, List<File> files) {
File source = new File(oldPath);
String suffixName = source.getName().substring(source.getName().lastIndexOf("."));
File filePath = new File(rootFileDir + File.separator + "exPort" + File.separator +generatorDataFileName());
String reName = "";
try {
if (!filePath.exists()) {
filePath.mkdirs();
}
File dest =new File(filePath+File.separator+oneType + "-" + twoType + "-" + count + suffixName);
Files.copy(source.toPath(), dest.toPath(), StandardCopyOption.REPLACE_EXISTING);
files.add(dest);
reName = dest.getPath();
} catch (IOException e) {
e.printStackTrace();
}
return reName;
}
//获取日期目录
private String generatorDataFileName() {
Calendar date = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
return format.format(date.getTime());
}
来源:https://blog.csdn.net/Hyper_Simon/article/details/51043556
标签:Java,复制文件,命名
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
一篇文章带你了解C语言二分查找
2023-10-16 19:51:57
![](https://img.aspxhome.com/file/2023/6/114936_0s.gif)
java项目构建Gradle的使用教程
2023-06-07 19:17:41
![](https://img.aspxhome.com/file/2023/9/84649_0s.png)
C语言非递归后序遍历二叉树
2023-12-13 18:05:45
![](https://img.aspxhome.com/file/2023/9/124039_0s.jpg)
Intellij IDEA 2017新特性之Spring Boot相关特征介绍
2023-06-22 15:13:02
![](https://img.aspxhome.com/file/2023/0/88140_0s.gif)
Android 判断日期是否在一年以内的算法实例
2023-03-22 06:04:13
Spring Cloud Gateway 服务网关的部署与使用详细讲解
2022-04-09 01:20:00
![](https://img.aspxhome.com/file/2023/6/132196_0s.png)
c#实现16进制和字符串之间转换的代码
2023-07-22 08:28:09
Java+Nginx实现POP、IMAP、SMTP邮箱代理服务
2023-11-26 10:31:47
![](https://img.aspxhome.com/file/2023/7/59327_0s.jpg)
java 中 poi解析Excel文件版本问题解决办法
2023-11-15 16:49:45
java list,set,map,数组间的相互转换详解
2023-04-11 13:02:04
C#判断字符串是否存在字母及字符串中字符的替换实例
2022-04-15 03:49:48
java中map和对象互转工具类的实现示例
2023-06-05 11:08:30
![](https://img.aspxhome.com/file/2023/2/62162_0s.png)
C#使用ToUpper()与ToLower()方法将字符串进行大小写转换的方法
2023-06-27 05:51:45
Maven本地打包war包实现代码解析
2021-09-06 12:51:14
![](https://img.aspxhome.com/file/2023/0/64500_0s.png)
Json操作库DynamicJson使用指南
2023-06-17 10:17:35
Java简单实现UDP和TCP的示例
2021-08-02 14:57:48
![](https://img.aspxhome.com/file/2023/1/66961_0s.png)
Android开发之子线程操作UI的几种方法
2022-08-05 23:17:52
浅谈Java中IO和NIO的本质和区别
2023-11-01 01:44:08
![](https://img.aspxhome.com/file/2023/4/86734_0s.jpg)
Unity3d使用FairyGUI 自定义字体的操作
2022-05-29 16:57:27
![](https://img.aspxhome.com/file/2023/4/122734_0s.png)
Java查找并高亮PDF文本过程解析
2022-02-17 07:20:32
![](https://img.aspxhome.com/file/2023/8/75508_0s.png)