欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

批量修改文件后缀名

程序员文章站 2022-03-03 12:03:12
...
import java.io.File;
import java.io.IOException;

public class ModifyFileSuffix {

static String path ="G:\\song\\XJ803GBJ";
public static void main(String[] args) {
// TODO Auto-generated method stub

modifiFileSuffix(path);
}

/**
* 批量修改文件后缀
* @param path
*/
static void modifiFileSuffix(String path) {
File file = new File(path);
try {
System.out.println(file.getCanonicalPath());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(file.isDirectory()) {
File[] files = file.listFiles();
for(File file1:files) {
try {
modifiFileSuffix(file1.getCanonicalPath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}else {
try {
String filePath = file.getCanonicalPath();
if(filePath.contains(".pdf")) {
filePath=filePath.replace(".pdf", ".mp3");
}
File dstFile = new File(filePath);
file.renameTo(dstFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

/**
* 批量删除某个文件
* @param path
*/
static void deleteFile(String path) {
File file = new File(path);
try {
System.out.println(file.getCanonicalPath());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(file.isDirectory()) {
File[] files = file.listFiles();
for(File file1:files) {
try {
modifiFileSuffix(file1.getCanonicalPath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}else {
try {
String filePath = file.getCanonicalPath();
if(filePath.contains(".bat")) {
file.delete();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
}