Java小工具 根据文本批量修改文件名
功能
可以根据使用路径修改文件名,已经测试,可以成功运行
思路
先是读取到txt文本文件,之后使用String的spilt进行分割,每一行的格式为 旧名字 新名字,中间的空格可以使用|或者其他字符代替,以此为标志分割String
之后将旧名字当做key,新名字当做value写入到map中去
获得文件的所在的文件夹,listFile遍历得到所有的文件,之后getName获得文件名(这里获得到的文件名是包括有扩展名的)
再次使用String的spilt进行处理(参数为"\.",需要转义),得到文件名和扩展名
reName改名字,参数为一个文件对象
代码
先把代码贴出来吧,之后再做个有界面的工具~
class Test {
private static Map<String, String> map;
public static void main(String[] args) {
map = readTxtFile("T:\\游戏资源\\仙剑4\\音乐目录.txt");
reName("T:\\游戏资源\\仙剑4\\仙剑奇侠传四音乐");
}
/**
*
* @param s 文件所在文件夹路径名
*/
public static void reName(String s){
File file = new File(s);
if (file.isDirectory()){
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
String h = files[i].getName();
String[] temp = h.split("\\.");
String newName = map.get(temp[0]);
files[i].renameTo(new File(s+"\\"+newName+"."+temp[temp.length-1]));
}
}
}
private static HashMap<String, String> readTxtFile(String filePath){
HashMap<String, String> map = new HashMap<>();
try {
String encoding="GBK";
File file=new File(filePath);
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){
String[] split = lineTxt.split(" ");
map.put(split[0],split[1]);
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
return map;
}
}
Java小工具 根据文本批量修改文件名的更多相关文章
- 使用Java中File类批量创建文件和批量修改文件名
批量创建文件 int cont = 1; String s = "E:\\学习资料\\Java笔记-"; while(cont<100){ File f = new File ...
- rename 批量修改文件名
1.rename的用法 rename与mv的区别就是mv只能对单个文件重命名,而rename可以批量修改文件名 linux中的rename有两种版本,一种是C语言版的,一种是Perl版的.早期的Lin ...
- Linux下find一次查找多个指定类型文件,指定文件或者排除某类文件,在 GREP 中匹配多个关键 批量修改文件名等
http://blog.sina.com.cn/s/blog_62e7fe670101dg9d.html linux下二进制文件查找: strings 0000.ts | grep -o " ...
- [转载][记录]shell 批量修改文件名
参考了:[新手入门] shell脚本批量修改文件名 4楼回复 我刚好是在vagrant+ubuntu中进行开发,windows手动修改太麻烦. #!/bin/ksh ls *.htm | while ...
- linux批量修改文件名的shell脚本
linux中批量修改文件名的shell脚本代码,主要是使用了rename,结合shell,喜欢的朋友可以参考下 使用 rename 命令 ======================== NAME ...
- 用python批量修改文件名
从youtube上当下来百来首mv,每个都需要去掉视频,这还挺好弄得,格式工厂一弄就完了,但是文件名,都带有乱七八糟的*啥的巴拉巴拉的,咋修改啊,几百首总不可能一个一个rename吧 #批量修改文件名 ...
- windows下如何批量修改文件名
windows下如何批量修改文件名 一.总结 一句话总结:就是用excel生成了多条修改文件名的dos命令然后执行,比较核心的就是把图片名称导入excel 将图片名称导入excel---编写如下DOS ...
- C# 批量修改文件名
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- Linux下批量修改文件名(rename)
原文地址: http://blog.csdn.net/sea_shore/article/details/6102437 1.rename命令批量修改文件名, 其实linux下可以使用别的办法来批量修 ...
随机推荐
- 下载 mysql 数据库 的步骤 完整版
1. 官网(点这里)上下载 2. 3. 4. 5. 6. 7.
- 别以为真懂Openstack: 虚拟机创建的50个步骤和100个知识点(4)
六.Libvirt 对于Libvirt,在启动虚拟机之前,首先需要define虚拟机,是一个XML格式的文件 列出所有的Instance # virsh list Id Name ...
- SUSE12Sp3-Nginx安装
1.安装pcre(nginx 依赖) 把安装包pcre-8.12.tar.gz复制到服务器指定目录 tar -zxvf pcre-8.12.tar.gz # 解压 cd pcre-8.12 #进入目录 ...
- module.exports与exports,export与export default之间的关系和区别
首先我们要明白一个前提,CommonJS模块规范和ES6模块规范完全是两种不同的概念. CommonJS模块规范 Node应用由模块组成,采用CommonJS模块规范. 根据这个规范,每个文件就是一个 ...
- [Swift]LeetCode95. 不同的二叉搜索树 II | Unique Binary Search Trees II
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- [Swift]LeetCode111. 二叉树的最小深度 | Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- [Swift]LeetCode797. 所有可能的路径 | All Paths From Source to Target
Given a directed, acyclic graph of N nodes. Find all possible paths from node 0 to node N-1, and re ...
- php设计模式2
代理模式 <?php /** * 代理模式:为其他对象提供一个代理以控制这个对象的访问 它是给某一个对象提供一个替代者,使之在client对象和subject对象之间编码更有效率. 代理可以提供 ...
- HBase之CF持久化系列(续1)
这一节本来打算讲解HRegion的初始化过程中一些比较复杂的流程.不过,考虑前面的博文做的铺垫并不够,因此,在这一节,我还是特意来介绍HBase的CF持久化.关于这个话题的整体流程性分析在博文< ...
- 【Sqoop篇】----Sqoop从搭建到应用案例
一.前述 今天开始讲解Sqoo的用法搭建和使用.Sqoop其实功能非常简单.主要用于在Hadoop(Hive)与传统的数据库(mysql.postgresql...)间进行数据的传递,可以将一个关系型 ...