add: rename() in files module
This commit is contained in:
@@ -81,14 +81,16 @@
|
|||||||
|
|
||||||
复制文件。例如`files.copy("/sdcard/1.txt", "/sdcard/Download/1.txt")`。
|
复制文件。例如`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)
|
### files.rename(path, newName)
|
||||||
* path \<String\> 要重命名的原文件路径
|
* path \<String\> 要重命名的原文件路径
|
||||||
* newName \<String\> 要重命名的新文件名
|
* newName \<String\> 要重命名的新文件名
|
||||||
|
|
||||||
重命名文件,并返回是否重命名成功。例如`files.rename("/sdcard/1.txt", "2.txt")`。
|
重命名文件,并返回是否重命名成功。例如`files.rename("/sdcard/1.txt", "2.txt")`。
|
||||||
|
|
||||||
也可以用作文件移动,例如`files.rename("/sdcard/1.txt", "/sdcard/Download/1.txt")`会把1.txt文件从sd卡根目录移动到Download文件夹。
|
|
||||||
|
|
||||||
### files.renameWithoutExtension(path, newName)
|
### files.renameWithoutExtension(path, newName)
|
||||||
* path \<String\> 要重命名的原文件路径
|
* path \<String\> 要重命名的原文件路径
|
||||||
* newName \<String\> 要重命名的新文件名
|
* newName \<String\> 要重命名的新文件名
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ public class SharedPrefScriptFileList extends ScriptFileList {
|
|||||||
public void rename(int position, String newName, boolean renameFile) {
|
public void rename(int position, String newName, boolean renameFile) {
|
||||||
mScriptName.set(position, newName);
|
mScriptName.set(position, newName);
|
||||||
if (renameFile) {
|
if (renameFile) {
|
||||||
String newPath = PFile.renameWithoutExtension(mScriptPath.get(position), newName);
|
String newPath = PFile.renameWithoutExtensionAndReturnNewPath(mScriptPath.get(position), newName);
|
||||||
mScriptPath.set(position, newPath);
|
mScriptPath.set(position, newPath);
|
||||||
}
|
}
|
||||||
syncWithSharedPref();
|
syncWithSharedPref();
|
||||||
|
|||||||
@@ -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 file = new File(path);
|
||||||
File newFile = new File(file.getParent(), newName + "." + getExtension(file.getName()));
|
File newFile = new File(file.getParent(), newName + "." + getExtension(file.getName()));
|
||||||
file.renameTo(newFile);
|
file.renameTo(newFile);
|
||||||
return newFile.getAbsolutePath();
|
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) {
|
public static String getExtension(String fileName) {
|
||||||
int i = fileName.lastIndexOf('.');
|
int i = fileName.lastIndexOf('.');
|
||||||
if (i < 0 || i + 1 >= fileName.length() - 1)
|
if (i < 0 || i + 1 >= fileName.length() - 1)
|
||||||
|
|||||||
Reference in New Issue
Block a user