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 文件批量重命名
首先,我们选中一个文件夹 右键,或者回车,给一个文件夹改名 同时选中三个文件夹 右键,选中批量更改 弹出批量更改,进行更改 改好后点回车,就能看到效果了 继续操作,完成所有文件 ...
随机推荐
- 导出到CSV文件
一.手工导出导出 1.winform void DataGridViewToExcel(DataGridView dataGridView1) { SaveFileDialog saveFileDia ...
- drf框架 - 序列化组件 | ModelSerializer (查,增,删,改)
ModelSerializer 序列化准备: 配置 settings.py # 注册rest_framework框架 INSTALLED_APPS = [ ... 'rest_framework' ] ...
- js原型和原型链的问题
<script> //js原型和原型链的概念 functionperson(name){ this.name=name; } person.prototype.age=18; person ...
- Notepad++ 编译运行java,c,c++
1.Java NPP_SAVE cd $(CURRENT_DIRECTORY) D:\tibco\bw6\tibcojre64\1.8.0\bin\javac.exe "$(FILE_NAM ...
- (尚010)Vue列表的搜素和排序
1.test010.html <!DOCTYPE html><html lang="en"><head> <meta charset=&q ...
- (5)Go数组
数组 数组是同一种数据类型元素的集合. 在Go语言中,数组从声明时就确定,使用时可以修改数组成员,但是数组大小不可变化. 基本语法: // 定义一个长度为3元素类型为int的数组a var a [3] ...
- Multi-Temporal SAR Data Large-Scale Crop Mapping Based on U-Net Model(利用U-net对多时相SAR影像获得作物图)
对哨兵1号的多时相双极化SAR数据进行预处理,得到18个日期的VV和VH共36景影像,通过ANOVA和JM距离分析,选其中ANOVA得到的F值最高的6景影像.真值用LC8数据和地面调查,目视解译得到标 ...
- hive(2)数据类型和文件格式
基本的数据类型 Hive支持关系型数据中大多数基本的数据类型,同时也支持关系型数据库中很少出现的三种集合数据类型. 集合数据类型 Hive中的列支持使用struct.map.array集合数据类型,下 ...
- 为什么要监控sql语句,以及如何监控,都有哪几种方式可以监控。
快速阅读 为什么要监控sql语句,以及如何监控,都有哪几种方式可以监控. 我们知道sql server 中有个工具叫sql profile ,可以实时监控sql server中 执行的sql 语句,以 ...
- 关于SQL中的ROWNUM问题
前言 昨天改小程序的后台,看见之前写的分页很奇怪,startIndex和endIndex两个下标, endIndex 总是在里面层,而startIndex总是在外层,我随后改了,直接Where row ...