将files文件夹下的所有文件名后加个.zip的后缀。
代码如下:
import java.io.File;
public class RenameFile {
public static void main(String args[]) {
File f = new File("/home/tom/test/files");
if (f.exists()) {
if (f.isDirectory()) {
File[] files = f.listFiles();
for (int i = 0; i < files.length; i++) {
String oldname = files[i].getName();
String newname = oldname.substring(0, oldname.length());
newname = newname + ".zip";
if (!oldname.equals(newname)) {
String path = files[i].getParent();
File newfile = new File(path + "/" + newname);
if (newfile.exists()) {
System.out.println(newname + "已经存在!");
} else {
files[i].renameTo(newfile);
}
}
}
}
} else {
System.out.println("File does not exist!");
}
}
}