add: rename() in files module

This commit is contained in:
hyb1996
2017-07-30 16:15:11 +08:00
parent fed7b97deb
commit d44a7921df
3 changed files with 22 additions and 4 deletions

View File

@@ -215,13 +215,29 @@ public class PFile {
}
}
public static String renameWithoutExtension(String path, String newName) {
public static String renameWithoutExtensionAndReturnNewPath(String path, String newName) {
File file = new File(path);
File newFile = new File(file.getParent(), newName + "." + getExtension(file.getName()));
file.renameTo(newFile);
return newFile.getAbsolutePath();
}
public static boolean renameWithoutExtension(String path, String newName) {
File file = new File(path);
File newFile = new File(file.getParent(), newName + "." + getExtension(file.getName()));
return file.renameTo(newFile);
}
public static boolean rename(String path, String newName) {
File f = new File(path);
return f.renameTo(new File(f.getParent(), newName));
}
public static boolean move(String path, String newPath) {
File f = new File(path);
return f.renameTo(new File(newPath));
}
public static String getExtension(String fileName) {
int i = fileName.lastIndexOf('.');
if (i < 0 || i + 1 >= fileName.length() - 1)