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

@@ -81,14 +81,16 @@
复制文件。例如`files.copy("/sdcard/1.txt", "/sdcard/Download/1.txt")`
### files.move(fromPath, toPath)
移动文件,返回是否移动成功。例如`files.rename("/sdcard/1.txt", "/sdcard/Download/1.txt")`会把1.txt文件从sd卡根目录移动到Download文件夹。
### files.rename(path, newName)
* path \<String\> 要重命名的原文件路径
* newName \<String\> 要重命名的新文件名
重命名文件,并返回是否重命名成功。例如`files.rename("/sdcard/1.txt", "2.txt")`
也可以用作文件移动,例如`files.rename("/sdcard/1.txt", "/sdcard/Download/1.txt")`会把1.txt文件从sd卡根目录移动到Download文件夹。
### files.renameWithoutExtension(path, newName)
* path \<String\> 要重命名的原文件路径
* newName \<String\> 要重命名的新文件名

View File

@@ -78,7 +78,7 @@ public class SharedPrefScriptFileList extends ScriptFileList {
public void rename(int position, String newName, boolean renameFile) {
mScriptName.set(position, newName);
if (renameFile) {
String newPath = PFile.renameWithoutExtension(mScriptPath.get(position), newName);
String newPath = PFile.renameWithoutExtensionAndReturnNewPath(mScriptPath.get(position), newName);
mScriptPath.set(position, newPath);
}
syncWithSharedPref();

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)