`
cyhmna
  • 浏览: 32597 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

批量修改文件后缀名

    博客分类:
  • java
阅读更多
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();
}

}
}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics