如何按编号排列和重命名文件?
How to arrange and rename files according to number?
我想先排列文件,然后按照文件名中的数字指定的顺序重命名。
例如:
我有一个包含一堆不同文件的文件夹。文件名由其末尾的数字表示。假设我们在该文件夹中有以下文件:
file_1.xml // Remains unchanged
file_2.xml // Remains unchanged
file_4.xml // Should be renamed to "file_3.xml"
file_9.xml // Should be renamed to "file_4.xml"
file_12.xml // Should be renamed to "file_5.xml"
我该怎么做?我想创建一个可靠的清理方法,按顺序重命名文件。
到目前为止:
private void updateFilesName() {
for (int i = 1; i <= filesAmount; i++) {
File file1 = new File(getFilesDir().getParent() + "/file_" + i + ".xml");
File file2 = new File(getFilesDir().getParent() + "/file_" + String.valueOf(i + 1) + ".xml");
if (!file1.exists() && file2.exists()) {
file2.renameTo(file1);
}
}
}
但这仅适用于 2 个文件位置之间的差异为 1。(如 file_2
和 file_4
之间)此方法不适用于 file_9
和 file_12
.
// Iterate all files under the directory and check the file name
File folder = new File("urdirectory");
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
if (!listOfFiles[i].getName().equals("file_" + (i+1) + ".xml")) {
File tempFile1 = listOfFiles[i];
File tempFile2 = new File("urdirectory" + "/file_" + String.valueOf(i + 1) + ".xml");
tempFile.renameTo(tempFile2);
}
}
private void updateFilesName() {
int j;
for (int i = 1; i <= filesAmount; i++) {
File file1 = new File(getFilesDir().getParent() + "/file_" + i + ".xml");
if (!file1.exists()) {
j = i+1;
while (!(new File(getFilesDir().getParent() + "/file_" + j + ".xml")).exists()) {
j++;
}
(new File(getFilesDir().getParent() + "/file_" + j + ".xml")).renameTo(file1);
}
}
}
我想先排列文件,然后按照文件名中的数字指定的顺序重命名。
例如:
我有一个包含一堆不同文件的文件夹。文件名由其末尾的数字表示。假设我们在该文件夹中有以下文件:
file_1.xml // Remains unchanged
file_2.xml // Remains unchanged
file_4.xml // Should be renamed to "file_3.xml"
file_9.xml // Should be renamed to "file_4.xml"
file_12.xml // Should be renamed to "file_5.xml"
我该怎么做?我想创建一个可靠的清理方法,按顺序重命名文件。
到目前为止:
private void updateFilesName() {
for (int i = 1; i <= filesAmount; i++) {
File file1 = new File(getFilesDir().getParent() + "/file_" + i + ".xml");
File file2 = new File(getFilesDir().getParent() + "/file_" + String.valueOf(i + 1) + ".xml");
if (!file1.exists() && file2.exists()) {
file2.renameTo(file1);
}
}
}
但这仅适用于 2 个文件位置之间的差异为 1。(如 file_2
和 file_4
之间)此方法不适用于 file_9
和 file_12
.
// Iterate all files under the directory and check the file name
File folder = new File("urdirectory");
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
if (!listOfFiles[i].getName().equals("file_" + (i+1) + ".xml")) {
File tempFile1 = listOfFiles[i];
File tempFile2 = new File("urdirectory" + "/file_" + String.valueOf(i + 1) + ".xml");
tempFile.renameTo(tempFile2);
}
}
private void updateFilesName() {
int j;
for (int i = 1; i <= filesAmount; i++) {
File file1 = new File(getFilesDir().getParent() + "/file_" + i + ".xml");
if (!file1.exists()) {
j = i+1;
while (!(new File(getFilesDir().getParent() + "/file_" + j + ".xml")).exists()) {
j++;
}
(new File(getFilesDir().getParent() + "/file_" + j + ".xml")).renameTo(file1);
}
}
}