Java根据文本内容,批量修改文件名称

这两天学到IO流对文件的操作,想起在几年前有几百个按"1,2,3"排序命名的短文,于是产生将其批量命名后整理的想法.

这批文本的名称在文件内第十行的位置,前面的是广告和其他不相关的东西

本想构造抓到第九行广告语后返回下一行文本的方法,没能实现,只好用了更简单直接的直接抓第十行

 1 package mycharset;
 2 
 3 import javax.naming.Name;
 4 import java.io.*;
 5 import java.nio.charset.StandardCharsets;
 6 import java.util.Arrays;
 7 //批量修改文件名
 8 public class CharSetDemo3 {
 9     //.getBytes("GBK"), "UTF-8"
10     public static void main(String[] args) throws IOException {
11         //输入要修改的路径
12         File file = new File("Z:\\AAAA\\TEST");
13         //System.out.println(file.renameTo(newfile));
14         //System.out.println(file.getAbsoluteFile());
15         Rename(file);
16     }
17 
18     //遍历文件夹 修改文件名
19     public static void Rename(File path) throws IOException {
20         File[] files = path.listFiles();
21         for (File file : files) {
22             if(file!=null){
23                 if(file.isFile()){
24                     String name = GetName(file.getAbsolutePath());
25                     File newfile = new File("Z:\\AAAA\\TEST\\"+name);
26                     file.renameTo(newfile);
27                     System.out.println("修改名称为:"+newfile);
28                 }
29             }
30         }
31     }
32 
33     //传入路径的字符串,输出名字字符串
34     public static String GetName(String path) throws IOException {
35         FileReader fr = new FileReader(path);
36         int flag = 0;
37         int len = fr.read();
38         String temp = "";
39         a:
40         while (true) {
41             temp = "";
42             while (len != 10) {
43                 temp += String.valueOf((char) len);
44                 len = fr.read();
45             }
46             //获取名称的规则  得到第十行数据
47             len = fr.read();
48             if (flag++ > 9) {
49                 //System.out.println(temp);
50                 break a;
51             }
52         }
53         //将最后一位回车byte删除后return
54         fr.close();
55         int len1 = temp.getBytes().length - 1;
56         byte[] bytes = temp.getBytes();
57         byte[] bytes1 = new byte[len1];
58         System.arraycopy(bytes, 0, bytes1, 0, len1);
59         String name = new String(bytes1);
60         return name + ".txt";
61     }
62 }
View Code

经过一上午的测试和修改,确实能达成目标,虽然局限性和方法都很差劲,但好歹能跑

其中遇到了两个问题

一是通过GetName方法获取的字符串名称无法在其后添加任何字符,比如抓到了"惊悚乐园",想要在其后加上".txt"变为完整名称,却在拼接后只显示".txt"

第一反应是前面的基础知识不牢靠,String方法用错了,但之后不管用Stringbuffer还是Stringbuild操作都无法成功,测试用创建的其他字符串却没有这种错误

那么只可能是GetName获得的字符串出现了问题,先想到的是编码问题,统一将编码和解码格式全改为UTF-8,还是失败

陷入无措后索性用byte数组提出来看一下抓的到底是什么格式的什么东西,结果在最后看到个"13",合着全被换行符整蛊了

又加了个很低效的代码去将最后换行符去掉,再次运行后,成功

第二个问题是输出后的名称为乱码,原本的文本都是ANSI编码的,感觉应该还是编码和转码的问题,应该再写个批量转换文本编码的方法的

然而并不会写,哈哈

直接去网上下了个批量转码的工具,转成UTF-8后再次运行,成功

 

总体感觉就是在一个残疾人身上不断装劣等义体,最后确实跑起来也实现了目的,但丑陋到自己都不想去读第二遍

而且这才半天过去,再看已经看不懂了

sad

 

 

结果晚上看完后半截网课才发现  用缓冲流的话会简单很多  遂将最丑陋的一部分代买修改

GetName方法实现了rea到指定字符串的行数后  将下一行return

 1 import java.io.*;
 2 //批量修改文件名
 3 public class copy2 {
 4     //.getBytes("GBK"), "UTF-8"
 5     public static void main(String[] args) throws IOException {
 6         //输入要修改的路径
 7         File file = new File("Z:\\AAAA\\TEST");
 8         Rename(file);
 9     }
10 
11     //遍历文件夹 修改文件名
12     public static void Rename(File path) throws IOException {
13         File[] files = path.listFiles();
14         for (File file : files) {
15             if (file != null) {
16                 if (file.isFile()) {
17                     String name = GetName(file.getAbsolutePath());
18                     File newfile = new File("Z:\\AAAA\\TEST\\" + name);
19                     System.out.println(file.renameTo(newfile));
20                     System.out.println("修改名称为:" + name);
21                 }
22             }
23         }
24     }
25     public static String GetName(String path) throws IOException {
26         BufferedReader br = new BufferedReader(new FileReader(path));
27         //循环 直到与关键语句相匹配后break
28         while (true) {
29             String line = br.readLine();
30             if (line != null && line.equals("也希望热心的网友能帮忙宣传下!"))
31                 break;
32         }
33         String name = br.readLine()+".txt";
34         br.close();
35         return name;
36     }
37 }
View Code

碰到的一个小问题是前两次运行无法实现修改

原因为直接return了br.readLine+".txt"  没有将BufferedReader关闭导致占用

 

结果第二天又看到了转变编码的方法   就又加了个方法进去   但没能实现先判断再转换   而是统一将GBK转为UTF-8

而且重新编码还是先建新文件后删除旧文件  重命名时也是建新的再删旧的  导致效率很低

放一起不方便测试  还是那句话  能跑就行  大概

 1 import java.io.*;
 2 import java.nio.charset.Charset;
 3 
 4 //批量修改文件名
 5 public class copy2 {
 6     //.getBytes("GBK"), "UTF-8"
 7     public static void main(String[] args) throws IOException {
 8         //输入要修改的路径
 9         File file = new File("Z:\\AAAA\\TEST");
10         //更改编码
11         changeByte(file);
12         //更改名字
13         Rename(file);
14     }
15 
16     //遍历文件夹 先修改文件编码 再修改文件名
17     public static void Rename(File path) throws IOException {
18         File[] files = path.listFiles();
19         for (File file : files) {
20             if (file != null) {
21                 if (file.isFile()) {
22                     String name = GetName(file.getAbsolutePath());
23                     File newfile = new File("Z:\\AAAA\\TEST\\" + name);
24                     System.out.println(file.renameTo(newfile));
25                     System.out.println("修改名称为:" + name);
26                 }
27             }
28         }
29     }
30 
31     public static String GetName(String path) throws IOException {
32         BufferedReader br = new BufferedReader(new FileReader(path));
33         //循环 直到与关键语句相匹配后break
34         while (true) {
35             String line = br.readLine();
36             if (line != null && line.equals("也希望热心的网友能帮忙宣传下!"))
37                 break;
38         }
39         String name = br.readLine() + ".txt";
40         br.close();
41         return name;
42     }
43 
44     public static void changeByte(File path) throws IOException {
45         File[] files = path.listFiles();
46         for (File file : files) {
47             if (file != null) {
48                 if (file.isFile()) {
49                     int len;
50                     File newfile = new File(path, "new" + file.getName());
51                     FileReader fr = new FileReader(file, Charset.forName("GBK"));
52                     FileWriter fw = new FileWriter(newfile, Charset.forName("UTF-8"));
53                     while ((len = fr.read()) != -1) {
54                         fw.write((char) len);
55                     }
56                     fw.close();
57                     fr.close();
58                     file.delete();
59                 }
60             } else {
61                 changeByte(file);
62             }
63         }
64     }
65 }
View Code

 

posted @ 2023-11-05 17:04  风来三醒  阅读(134)  评论(0)    收藏  举报