1,需求,
将docx后缀的文件名修改为doc后缀
2,文件结构图示(这是执行后的效果图)
3,代码
public static void main(String[] args) {
try {
//文件地址
String pathname="D:\\project\\word\\";
File file = new File(pathname);
File[] fileList = file.listFiles();
//因为我的文件是三层的树状结构,所以使用三个for循环
for (int i = 0; i < fileList.length; i++) {
File file2 = new File(fileList[i].toString());
File[] fileList2 = file2.listFiles();
for (int j = 0; j <fileList2.length ; j++) {
File[] fileName3 = fileList2[j].listFiles();
for (int k = 0; k <fileName3.length ; k++) {
//旧后缀文件名
String fileName = fileName3[k].getName();
if (fileName.endsWith(".docx")) {
//新后缀文件名
fileName = fileName.substring(0, fileName.length()-1);
//更换后缀
fileName3[k].renameTo(new File(fileName3[k].getParent() + "\\" + fileName));
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
4,其他方法
按照以上方法,但是我没有成功,下次有时间再摸索一下