Hello I'm trying to get with numerical order all files that exist in a folder.
First i check a folder(main folder) the sub-folders that contained in. After that, for each sub-folder I'm getting the names of the files that exist in.
I wrote a code that does this job for me but when i print (the names of the files of a sub-folder) I'm not getting them with the correct order.(numerical).
For example i have my main Folder called 'test', in there exist 3 sub-folder named Sub1, Sub2, Sub3
FOLDER Test *contains* [ FOLDER SUB1 || FOLDER SUB2 || FOLDER SUB3 ]
Each sub-Fodler have files with names (1.txt , 2.txt ,.....,15.txt,16.txt,...,22.txt,...etc)
This code does this job... but...
import java.io.File;
import java.io.FileFilter;
public class Main {
public static void main(String[] args) {
File file = new File("C:\\test");
File[] files = file.listFiles(new FileFilter() {
public boolean accept(File f) {
String name=f.getName(); //read every subfodler name
System.out.println(name);
File folder = new File("C:\\test\\"+name); //for each subfolder
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
System.out.println("File " + listOfFiles[i].getName());
//print the names of files that included
} else if (listOfFiles[i].isDirectory()) {
System.out.println("Directory " + listOfFiles[i].getName());
}
}
return f.isDirectory();
}});
}
}
but the output is like that...
Folder1
File 1.txt
File 10.txt
File 11.txt
File 12.txt
File 13.txt
File 14.txt
File 2.txt
File 3.txt
File 4.txt
File 5.txt
File 6.txt
File 7.txt
File 8.txt
File 9.txt
Folder2
............... same order as Folder1 ... etc
How i could taking the file names with numerical order so the output that i will get will be like that:
Folder1
File 1.txt
File 2.txt
File 3.txt
File 4.txt
File 5.txt
File 6.txt
File 7.txt
File 8.txt
File 9.txt
File 10.txt
File 11.txt
File 12.txt
File 13.txt
File 14.txt