Java 重命名 class 不断删除 运行dom 图片 运行
Java Rename class keeps deleting random pictures when ran
顺便说一下,我有一个 linux 操作系统。
import java.io.*;
import java.text.DecimalFormat;
import java.awt.Desktop;
public class rename {
public static boolean renameTo(File Pictures){
File[] listofFiles = Pictures.listFiles();
boolean check = false;
if(listofFiles != null){
int count = 000;
for(File pic : listofFiles){
String filename = pic.getName();
String extention = filename.substring(filename.lastIndexOf(".")+1, filename.length());
String pictureExtention = "JPG";
if(extention.equals(pictureExtention)){
//we have picture, yay!
count++;
pic.renameTo(new File((new DecimalFormat("000").format(count))+".JPG"));
check=true;
}//end if
}//end for
}//end if
return check;
}
public static void main(String[] args) throws IOException {
String homePath = System.getProperty("user.home");
File home = new File(homePath);
File pictures = new File(home, "Test");
Desktop.getDesktop().open(pictures);
boolean x = renameTo(pictures);
System.out.println(x);
}
}//end class
所以当重命名很多JPG文件时,它会从文件中删除一些,我不明白为什么。有任何想法吗?我希望它将文件 001.JPG 重命名为(假设有 25 张图片)025.JPG。然而,它正确地重命名了那些没有被删除的。
File
class's listFiles
method 可以 return 它的文件以任何顺序排列。
There is no guarantee that the name strings in the resulting array will appear in any specific order; they are not, in particular, guaranteed to appear in alphabetical order.
一个文件可以有效的删除它之前的文件,例如。在“006.JPG”重命名之前,“005.JPG”重命名为“006.JPG”。
在重命名文件之前将文件按文件名降序排列以避免由于文件名冲突而导致的意外删除。
Arrays.sort(files, Comparator.comparing(File::getName).reversed());
打印你所拥有的和你得到的。我相信你会看到你拥有的和你想要的之间有交集。
例如,您有文件 001.JPG 并将文件 abc.JPG 重命名为 001.JPG
我认为这是原因,因为您实际上没有删除任何文件。
顺便说一下,我有一个 linux 操作系统。
import java.io.*;
import java.text.DecimalFormat;
import java.awt.Desktop;
public class rename {
public static boolean renameTo(File Pictures){
File[] listofFiles = Pictures.listFiles();
boolean check = false;
if(listofFiles != null){
int count = 000;
for(File pic : listofFiles){
String filename = pic.getName();
String extention = filename.substring(filename.lastIndexOf(".")+1, filename.length());
String pictureExtention = "JPG";
if(extention.equals(pictureExtention)){
//we have picture, yay!
count++;
pic.renameTo(new File((new DecimalFormat("000").format(count))+".JPG"));
check=true;
}//end if
}//end for
}//end if
return check;
}
public static void main(String[] args) throws IOException {
String homePath = System.getProperty("user.home");
File home = new File(homePath);
File pictures = new File(home, "Test");
Desktop.getDesktop().open(pictures);
boolean x = renameTo(pictures);
System.out.println(x);
}
}//end class
所以当重命名很多JPG文件时,它会从文件中删除一些,我不明白为什么。有任何想法吗?我希望它将文件 001.JPG 重命名为(假设有 25 张图片)025.JPG。然而,它正确地重命名了那些没有被删除的。
File
class's listFiles
method 可以 return 它的文件以任何顺序排列。
There is no guarantee that the name strings in the resulting array will appear in any specific order; they are not, in particular, guaranteed to appear in alphabetical order.
一个文件可以有效的删除它之前的文件,例如。在“006.JPG”重命名之前,“005.JPG”重命名为“006.JPG”。
在重命名文件之前将文件按文件名降序排列以避免由于文件名冲突而导致的意外删除。
Arrays.sort(files, Comparator.comparing(File::getName).reversed());
打印你所拥有的和你得到的。我相信你会看到你拥有的和你想要的之间有交集。
例如,您有文件 001.JPG 并将文件 abc.JPG 重命名为 001.JPG
我认为这是原因,因为您实际上没有删除任何文件。