博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用Java代码批量重命名文件
阅读量:7154 次
发布时间:2019-06-29

本文共 1556 字,大约阅读时间需要 5 分钟。

昨天做实验,采了很多图,由于使用了之前版本的采图程序,导致图片的命名全部错了,就是在小于10的数字前面没有加0,如02。导致接下来跑另一个程序的时候读图顺序总是出错,故写了一个代码批量重命名了这些文件。由于正则表达式不会写,只能用了最笨的方法来做。

import java.io.File;import java.util.ArrayList;import java.util.List;public class Test {	public static void main(String[] args) {		String path = "F:/20180924L1/1000hz_2"; // 要批量修改的文件所在的目录		File file = new File(path);		boolean isDirectory = file.isDirectory();		if (!isDirectory) { // 如果不是文件夹,就返回			System.out.println(path + "不是文件夹!");			return;		}		String[] files = file.list();		File f = null;		String newFileName = ""; // 新的文件名字		String oldFileName = ""; // 旧的文件名字		for (int i = 0; i < files.length; i++) { // 遍历该文件夹下的所有文件			oldFileName = files[i];			newFileName = oldFileName;			boolean x1 = false;			boolean x2 = false;			int a, b, c, d;			a = oldFileName.indexOf("_");			b = oldFileName.indexOf(".");			c = oldFileName.lastIndexOf("_");			d = oldFileName.indexOf(".", c);			String snum1 = oldFileName.substring(a + 1, b);			String snum2 = oldFileName.substring(c + 1, d);			if (Integer.parseInt(snum2) < 10) {				newFileName = newFileName.substring(0, c + 1) + "0" + newFileName.substring(c + 1);				x1 = true;			}			if (Integer.parseInt(snum1) < 10) {				newFileName = newFileName.substring(0, a + 1) + "0" + newFileName.substring(a + 1);				x2 = true;			}			// 如果文件名没变,跳过它			if (!(x1 || x2)) {				continue;			}			// 将修改后的文件保存在原目录下			f = new File(path + "/" + oldFileName);			f.renameTo(new File(path + "/" + newFileName));		}	}}复制代码

把一大批类似于

String s = "1000hz_12.0v1_6.0v2.jpg";复制代码

这样的文件名重命名为

"1000hz_12.0v1_06.0v2.jpg"复制代码

转载地址:http://uylgl.baihongyu.com/

你可能感兴趣的文章
ora-04031
查看>>
jquery -- onchange
查看>>
五大查找
查看>>
jquery天气预报
查看>>
Hadoop集群搭建伪分布式集群搭建
查看>>
c# (ENUM)枚举组合类型的谷歌序列化Protobuf
查看>>
BufferedReader.readLine() 使用方法
查看>>
SHELL脚本简单的赋值与递增
查看>>
990元外贸企业建站方案
查看>>
动态网页开发基础
查看>>
Xcode Build Search Paths设置
查看>>
json学习
查看>>
黄聪:wordpress自定义post_type,并且自定义固定链接
查看>>
as类收集(转)
查看>>
django补充
查看>>
0916编译原理第二次作业
查看>>
nodeType的十二种类型
查看>>
批处理脚本+adb命令(二)
查看>>
数据处理中白化Whitening的作用图解分析
查看>>
后缀数组da3模板
查看>>