I'm searching a directory of files with Java 8 and extracting music files. When I run my code on Linux (Debian Wheezy) it completes in around 20 seconds. However, when I run the identical code in Windows 8.1 (same machine!) it takes an inordinately long time, so long that it's really unusable. I've ascertained that the process is occurring as it should, just very slowly. In the time that the Linux variant finds all 2500 files, the Windows variant has found around 100.
Here is the code:
public int List(String path) throws InterruptedException, IOException {
//Linux Variant
if (HomeScreen.os.equals("Linux")) {
File root = new File(path);
File[] list = root.listFiles();
Arrays.sort(list);
if (list == null) {
return 0;
}
for (File f : list) {
if (f.isDirectory()) {
List(f.getAbsolutePath());
} else if (f.isFile()) {
String outPath = f.getAbsolutePath();
try {
String ext = outPath.substring(outPath.lastIndexOf(".") + 1);
if (ext.equals("wma") || ext.equals("m4a") || ext.equals("mp3")) {
String fulltrack = outPath.substring(outPath.lastIndexOf("Music/") + 6);
lm.addElement(fulltrack);
numbers++;
}
} catch (Exception e) {
System.out.println(outPath + " is not a valid file!!!!!");
}
HomeScreen.Library.setModel(lm);
}
}
//Windows variant
} else if (HomeScreen.os.equals("Windows 8.1")){
System.out.println("Using " + HomeScreen.os + " methods...");
File root = new File(path);
File[] list = root.listFiles();
Arrays.sort(list);
if (list == null) {
return 0;
}
for (File f : list) {
if (f.isDirectory()) {
List(f.getAbsolutePath());
} else if (f.isFile()) {
String outPath = f.getAbsolutePath();
try {
String ext = outPath.substring(outPath.lastIndexOf(".") + 1);
if (ext.equals("wma") || ext.equals("m4a") || ext.equals("mp3")) {
String fulltrack = outPath.substring(outPath.lastIndexOf("Music/") + 9);
lm.addElement(fulltrack);
numbers++;
}
} catch (Exception e) {
System.out.println(outPath + " is not a valid file!!!!!");
}
HomeScreen.Library.setModel(lm);
}
}
}
return numbers;
}
I'm still pretty new to Java, so I'm not sure how to go about optimising the code for Windows. Is there any way this can be sped up, or are Windows users doomed to go for a coffee and wait for the load up?
Incidentally, I've put this method in a thread when using Windows so that other things can be done whilst waiting, but this is most definitely not an ideal solution. The drive being searched is a 7200 rpm HDD and there is 8GB RAM installed.