java IO 文件批量重命名
java IO 文件批量重命名
package com.vfsd.renamefile; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; /*******************************************************************************************************
* Copyright: vc1.0 2018. All rights reserved. <br>
* The whole package including this class is licensed under <br>
* <br>
* @ClassName: <br>
* @Directory: <br>
* @author: lzb <br>
* @version: v1.0.0 <br>
* @date: <br>
*
* @Statement: If you are using the package or parts of it in any commercial way, a commercial license is required. <br>
* Visit <a href='http://www.bim-times.com'>http://www.bim-times.com</a> for more information.<br>
*
*********************************************************************************************************/
public class RenameFile { public static void main(String[] args) throws Exception {
/*
String fileDirStr = "D:\\�ο��ֲ�\\���\\";
renameFile(fileDirStr);
*/
//mergeFile(); String fileDir = "D:\\zy_img_20190916\\1\\";
String fileDir2 = "D:\\zy_img_20190916\\2\\"; renameFile2(fileDir,fileDir2);//图片拷贝并重命名
//renameFile3(fileDir,fileDir2);//json拷贝并重命名
} public static void renameFile(String fileDirStr) throws Exception{
File fileDir = new File(fileDirStr);
if(fileDir.isDirectory()) {
File[] filesList = fileDir.listFiles();
for(int i=0;i<filesList.length;i++) {
File fileIndex = filesList[i];
if(fileIndex.getName().indexOf(".jpg")>0 ||fileIndex.getName().indexOf(".png")>0 ||fileIndex.getName().indexOf(".jpeg")>0||fileIndex.getName().indexOf(".gif")>0) {
//String newName=fileDirStr+i+fileIndex.getName().substring(fileIndex.getName().length()-4, fileIndex.getName().length()); File fileNew = new File(fileDirStr+i+fileIndex.getName().substring(fileIndex.getName().length()-4, fileIndex.getName().length()));
fileIndex.renameTo(fileNew);
System.out.println(fileNew.getName());
}
}
}
} /**
* 图片文件拷贝并重命名
* @param fileDirStr
* @param fileDir2
* @throws Exception
*/
public static void renameFile2(String fileDirStr,String fileDir2) throws Exception{
File fileDir = new File(fileDirStr);
if(fileDir.isDirectory()) {
File[] filesList = fileDir.listFiles();
for(int i=0;i<filesList.length;i++) {
File fileIndex = filesList[i];
if(fileIndex.getName().indexOf(".jpg")>0 ||fileIndex.getName().indexOf(".png")>0 ||fileIndex.getName().indexOf(".jpeg")>0||fileIndex.getName().indexOf(".JPG")>0) {
//String newName=fileDirStr+i+fileIndex.getName().substring(fileIndex.getName().length()-4, fileIndex.getName().length()); File fileNew = new File(fileDir2+(i+1)+fileIndex.getName().substring(fileIndex.getName().length()-4, fileIndex.getName().length()));
fileIndex.renameTo(fileNew);
System.out.println(fileNew.getName());
}
}
}
} /**
* JSON文件拷贝并重命名
* @param fileDirStr
* @param fileDir2
* @throws Exception
*/
public static void renameFile3(String fileDirStr,String fileDir2) throws Exception{
File fileDir = new File(fileDirStr);
if(fileDir.isDirectory()) {
File[] filesList = fileDir.listFiles();
for(int i=0;i<filesList.length;i++) {
File fileIndex = filesList[i];
if(fileIndex.getName().indexOf(".json")>0) {
//String newName=fileDirStr+i+fileIndex.getName().substring(fileIndex.getName().length()-4, fileIndex.getName().length()); File fileNew = new File(fileDir2+(i+1)+fileIndex.getName().substring(fileIndex.getName().length()-5, fileIndex.getName().length()));
fileIndex.renameTo(fileNew);
System.out.println(fileIndex.getName()+" "+fileNew.getName());
}
}
}
} /**
* �Ѷ���ļ����е�ͼƬ�ϲ�
* @throws IOException
*/
public static void mergeFile() throws IOException {
//String dir1 = "D:\\�ο��ֲ�\\����\\��ע-2019\\�_�����ע\\mask_data\\";
//String dir1 = "D:\\�ο��ֲ�\\����\\��ע-2019\\humanandladder301-350_labeled\\mask_data\\";
//String dir1 = "D:\\�ο��ֲ�\\����\\��ע-2019\\humanandladder451-600\\mask_data\\";
//String dir1 = "D:\\�ο��ֲ�\\����\\��ע-2019\\humanandladder601-750\\mask_data\\";
//String dir1 = "D:\\�ο��ֲ�\\����\\��ע-2019\\humanandladder150-300_labeled\\mask_data\\";
//String dir1 = "D:\\�ο��ֲ�\\����\\��ע-2019\\maskedhumanandladder901-1050\\mask_data\\";
//String dir1 = "D:\\�ο��ֲ�\\����\\��ע-2019\\humanandladder751-900\\mask_data\\";
//String dir1 = "D:\\�ο��ֲ�\\����\\��ע-2019\\humanandladder1201-1350\\mask_data\\";
String dir1 = "D:\\�ο��ֲ�\\����\\��ע-2019\\humanandladder1051-1200\\mask_data\\"; File fileDir = new File(dir1);
File fileDirs[] = fileDir.listFiles(); for(int i=0;i<fileDirs.length;i++) {
File fileIndexDir = fileDirs[i];
if(fileIndexDir.isDirectory()) {
File fileLists[] = fileIndexDir.listFiles();
File fileMaskImg = new File(fileIndexDir.getPath()+"\\label.png");
if(fileMaskImg.exists()) {
System.out.println(fileIndexDir.getName().replace("_json", ""));
File newFile = new File("D:\\�ο��ֲ�\\����\\��ע-2019\\train_data_20190227\\cv2_mask\\"+fileIndexDir.getName().replace("_json", "")+".png");
copyFile(fileMaskImg,newFile);
}
} } } /**
* �����ļ�
* @param oldFile
* @param newFile
* @throws IOException
*/
public static void copyFile(File oldFile,File newFile) throws IOException{
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileInputStream = new FileInputStream(oldFile);
fileOutputStream = new FileOutputStream(newFile);
byte[] bytes = new byte[1024];
int index=0; while((index=fileInputStream.read(bytes))!=-1) {
fileOutputStream.write(bytes, 0, index);
}
}catch(Exception e) {
e.printStackTrace();
}finally {
fileOutputStream.close();
fileInputStream.close();
}
} }
java IO 文件批量重命名的更多相关文章
- Java实现文件批量重命名
Windows操作系统可以实现重命名文件操作,却不能实现批量重命名.本实例实现了批量重命名功能,可以将一个文件夹内同一类型的文件按照一定的规则批量重命名.用户可以给出重命名模板,程序可以根据模板对相应 ...
- Java 实现文件批量重命名亲测可用(精简版)
package com.cmge.utils; import java.io.*; import java.util.*; import java.util.Map.Entry; import com ...
- 使用java对文件批量重命名
有时候从网络上下载的电视剧或者动漫,名字上都会被该网站加上前缀或者后缀,如图: 那么处女座的同学就不同意了,不行,我就是想让它按照我的习惯方式命名!但是呢,一个个修改是不是特别麻烦,如果是上百个呢?如 ...
- C#实现文件批量重命名源码下载
本文要实现的功能是 文件批量重命名,当选择一个文件夹时,通过操作可以把文件夹下面所有文件进行重命名.建立了HoverTreeBatch项目. 然后 定义文件夹信息: DirectoryInfo _Th ...
- php文件夹下文件批量重命名
php文件夹下文件批量重命名 <?php header("Content-type:text/html;charset=utf-8"); $dir = __DIR__.'./ ...
- 使用 vim 快速对当前文件夹下的文件批量重命名
前言 我们在使用 Linux 的时候,有很多种方法可以对文件进行重命名,例如 命令行 下的 mv 命令,或者是使用像 dolphin 这样的图形文件管理器.但是有时候需要对一个文件夹下的所有文件批量重 ...
- 利用Python对文件批量重命名
由于要用到对无序的文件进行重命名,以下写了一个脚本,进行批量进行重命名. 基本格式是 i.后缀名 ( i 循环条件下的数 ) 1 #coding:utf8 2 import os; 3 4 def ...
- 【Linux】文件批量重命名
例如:我们现在有如下文件列表 我们现在想要把.log..LOG与.rar为扩展名的文件全部重新命名,具体脚本如下: #!/bin/sh ##------------------------------ ...
- Mac OS Yosemite 文件批量重命名
首先,我们选中一个文件夹 右键,或者回车,给一个文件夹改名 同时选中三个文件夹 右键,选中批量更改 弹出批量更改,进行更改 改好后点回车,就能看到效果了 继续操作,完成所有文件 ...
随机推荐
- Failed to bind properties under 'logging.level' to java.util.Map<java.lang.String, java.lang.String>
org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'log ...
- docker相关的一些指令
1. docker cp [容器id]:[容器文件的路径] [宿主机文件的路径] 2. docker exec -it [容器id] /bin/bash 3. docker commit -a [作者 ...
- HDFS集群中DataNode的上线与下线
在HDFS集群的运维过程中,肯定会遇到DataNode的新增和删除,即上线与下线.这篇文章就详细讲解下DataNode的上线和下线的过程. 背景 在我们的微职位视频课程中,我们已经安装了3个节点的HD ...
- mysql 端口号被占用
开始-运行-cmd, 输入 netstat -ano, 看第一列,后面的就是端口,找到3306 ,记住对应的PID!! 然后打开任务管理器 查看 -> 选择列 -> 勾上 PID(进 ...
- c99的新功能
在ANSI的标准确立后,C语言的规范在一段时间内没有大的变动,然而C++在自己的标准化创建过程中继续发展壮大.<标准修正案一>在1994年为C语言创建了一个新标准,但是只修正了一些C89标 ...
- 浅谈JS高阶函数
引入 我们都知道函数是被设计为执行特定任务的代码块,会在某代码调用它时被执行,获得返回值或者实现其他功能.函数有函数名和参数,而函数参数是当调用函数接收的真实的值. 今天要说的高阶函数的英文为High ...
- vue路由监听及路由守卫
路由监听: //当一个组件被复用的时候,那么路由发生变化,但是页面上面的数据不会发生变化 新建one.vue 组件 作为home的子组件,在home.vue 中写遍历渲染页面 ,并用params传参, ...
- Session的数据共享
要体现出Session的数据共享,需要建立两个Servlet: 第一个:建立Session,将值设置为Tom. protected void doGet(HttpServletRequest requ ...
- 2019暑期金华集训 Day7 分治
自闭集训 Day7 分治 主定理 由于我沉迷调题,这个地方没听课. 某些不等式 咕了 nth_element 使用快速排序的思想,选一个中间点,看左右有多少个. 期望复杂度\(O(n)\). 首先把一 ...
- ELK教程1:ElasticSearch集群的部署ELK
在分布式系统中,应用数量众多,应用调用链复杂,常常使用ELK作为日志收集.分析和展示的组件.本篇文章将讲讲解如何部署ELK,然后讲解如何使用Filebeat采集Spring Boot的日志输出到Log ...