String realPath = "F:/Projects/prj_2010/test.jpeg";
String folderPath = realPath.substring(0, realPath.lastIndexOf("//"));
File file = new File(folderPath);
String dirPath = file.getAbsolutePath();//目录路径
if (file.isDirectory()) {
File[] files = file.listFiles();//获取此目录下的文件列表
for (File fileFrom : files) {
String fromFile = fileFrom.getName();//得到单个文件名
if (fromFile.endsWith(".x")) {
fromFile = fromFile.substring(0, fromFile.lastIndexOf(".x"));
String toFileName = dirPath + "/" + fromFile;
File toFile = new File(toFileName);
if (fileFrom.exists() && !toFile.exists()) {
//开始更名
fileFrom.renameTo(toFile);
}
}
}
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/16355.html