Reputation: 1089
I'm trying to get a list of all mp3 files in a directory (the subdir named music in the directory of the applet) so that I then can their names in a JavaScript function to push them into an array.
Everything works, but the listing process... It only return the very first mp3 file in the directory, not the other ones...
This is my code
JAVA:
import java.applet.Applet;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
public class Main extends Applet {
private static final long serialVersionUID = 1L;
public void init() {
File[] lib = getFiles(new File((getCodeBase() + File.separator + "music").substring(6)));
for (File s:lib) {
if (s.getName().substring(s.getName().length() - 3).equalsIgnoreCase("mp3")) {
try {getAppletContext().showDocument(new URL("javascript:addSong('"+s.getName()+"')"));}
catch (MalformedURLException me) {}
}
}
try {getAppletContext().showDocument(new URL("javascript:init()"));}
catch (MalformedURLException me) {}
}
public File[] getFiles(File dir) {
return dir.listFiles();
}
}
JavaScript:
function addSong(s) {
// Adding to array
window.songs.push("music/" + s);
// Debug message
alert(s);
}
function init() {
// Random code to initialze music player
// getting and listing values from "songs" which got content form addSong()
}
Upvotes: 1
Views: 1298
Reputation: 8012
I have made slight changes in your code. Check it out. This should resolve your issue:
import java.applet.Applet;
import java.io.File;
import java.io.FilenameFilter;
import java.net.MalformedURLException;
import java.net.URL;
public class Main extends Applet {
private static final long serialVersionUID = 1L;
@Override
public void init() {
File[] lib = getMP3Files("E:/Music/BollywoodMusic"); // pass your directory name here
for(File s:lib) {
try { getAppletContext().showDocument(new URL("javascript:addSong('"+s.getName()+"')")); } catch(MalformedURLException me) {}
}
try {getAppletContext().showDocument(new URL("javascript:init()"));}
catch (MalformedURLException me) {}
}
public static File[] getMP3Files(String directoryName) {
File directory = new File(directoryName);
return directory.listFiles(new FilenameFilter() {
public boolean accept(File directory, String fileName) {
return fileName.endsWith(".mp3"); } });
}
}
Upvotes: 1
Reputation: 29806
To list(recursively) all files(with .mp3 extension) in a given directory I have this following code:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class FileLister {
private List<File> getFileList(File startingDir) throws FileNotFoundException {
List<File> result = new ArrayList<File>();
File[] filesAndDirs = startingDir.listFiles();
List<File> filesDirs = Arrays.asList(filesAndDirs);
for (File file : filesDirs) {
result.add(file);
if (!file.isFile()) {
List<File> deeperList = getFileList(file);
result.addAll(deeperList);
}
}
return result;
}
public static void main(String[] args) throws FileNotFoundException {
for(File file : new FileLister().getFileList(new File("D:\\Music"))){
if(file.getName().contains(".")) {
String extension = file.getName().substring(file.getName().lastIndexOf("."), file.getName().length());
if(extension.equalsIgnoreCase(".mp3")) {
System.out.println(file.getName());
}
}
}
}
}
Upvotes: 1
Reputation: 843
I tried your code (on eclipse) and it works fine, be sure u putted the files in the right directory
and i can't get why u are using .substring(6)
Upvotes: 0