Reputation: 968
I dont know what is happening to me. I want to access a directory with multiple files in it suppose:
folder\\1.txt 2.txt 3.txt....
now I want to read them according to their occurrence I mean first comes the lowest simply I want to read them in ascending order! My code is:
File f=new File("xxx");
File[] files = f.listFiles();
for (File ff : files) {
if(ff.isFile()) {
System.out.println(ff.toString());
}
}
so far the code works fine but it takes the files as
1.txt
10.txt
11.txt
9.txt
8.txt
...
so what is going wrong i want to read them orderly in ascending order
Upvotes: 0
Views: 257
Reputation:
just sort by Arrays.sort:
Arrays.sort(files, new Comparator<File>() {
public int compare(File o1, File o2) {
int n1 = getNum(o1.getName());
int n2 = getNum(o2.getName());
return n1 - n2;
}
}
private int getNum(String name) {
int i;
// extract number from the file name here by doing some processes
return i;
}
Upvotes: 1