I have a substantial music collection, which will not load entirely in any music program.
So I wrote this quick program, to play a random song in a subfolder of my music directory.
I'm just wondering if anyone can improve on this code? It works pretty well, but the subdirectories are not all equal with respect to size, so it neglects some directories. My random mod might also be a problem. This is just to have a useful shuffle.
import java.io.File;
import java.io.IOException;
public class Pick {
public static void main(String args[]) throws IOException {
File f = new File("C:\\MUSIC");
File random = null;
try {
random = goDeeper(f);
} catch (Exception e) {
}
while (random == null) {
try {
random = goDeeper(f);
} catch (Exception e) {
}
}
System.out.println(random.getAbsolutePath());
Process myProcess = Runtime.getRuntime().exec(
"\"C:\\Program Files (x86)\\foobar2000\\foobar2000.exe\" \""
+ random.getAbsolutePath() + "\" ");
}
public static File goDeeper(File f) {
File[] children = f.listFiles();
int rand = (int) ((Math.random() * 123456) % children.length);
File random = children[rand];
if (random.isDirectory()) {
return goDeeper(new File(f.getAbsolutePath() + "/"
+ random.getName()));
} else if (random.isFile() && random.getName().endsWith("mp3"))
return random.getAbsoluteFile();
return null;
}
}