Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java列出目录中的文件_Java_Javascript_Applet_Directory_Subdirectory - Fatal编程技术网

Java列出目录中的文件

Java列出目录中的文件,java,javascript,applet,directory,subdirectory,Java,Javascript,Applet,Directory,Subdirectory,我试图获得一个目录中所有mp3文件的列表(applet目录中名为music的子目录),这样我就可以在JavaScript函数中将它们的名称放入一个数组中 一切正常,但上市过程。。。它只返回目录中的第一个mp3文件,而不返回其他文件 这是我的密码 爪哇: JavaScript: function addSong(s) { // Adding to array window.songs.push("music/" + s); // Debug message aler

我试图获得一个目录中所有mp3文件的列表(applet目录中名为music的子目录),这样我就可以在JavaScript函数中将它们的名称放入一个数组中

一切正常,但上市过程。。。它只返回目录中的第一个mp3文件,而不返回其他文件

这是我的密码

爪哇:

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()
}
我尝试了你的代码(在eclipse上),效果很好,请确保你将文件放在了正确的目录中

我不明白你为什么要使用
.substring(6)

来(递归地)列出给定目录中的所有文件(扩展名为.mp3)。我有以下代码:

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());
                }
            }
        }
    }
}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.ArrayList;
导入java.util.array;
导入java.util.List;
公共类文件列表器{
私有列表getFileList(File startingDir)引发FileNotFoundException{
列表结果=新建ArrayList();
File[]filesAndDirs=startingDir.listFiles();
List filesDirs=Arrays.asList(filesAndDirs);
for(文件:filesDirs){
结果.添加(文件);
如果(!file.isFile()){
List deeperList=getFileList(文件);
结果:addAll(deeperList);
}
}
返回结果;
}
公共静态void main(字符串[]args)引发FileNotFoundException{
用于(文件:新文件列表器().getFileList(新文件(“D:\\Music”)){
if(file.getName()包含(“.”){
字符串扩展名=file.getName().substring(file.getName().lastIndexOf(“.”),file.getName().length());
if(扩展名.equalsIgnoreCase(“.mp3”)){
System.out.println(file.getName());
}
}
}
}
}

我对您的代码做了一些细微的更改。过来看。这将解决您的问题:

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"); } });
    }

}

您是否尝试过从服务此小程序的主机以外的其他主机执行此小程序?您无法使用
new File()
@JBNizet()访问服务器上的文件。我正要说同样的话,但既然您已经说过了,我将投票表决该评论,作为第一个明智的回答。“我正在本地尝试”那又怎么样?让它在本地工作,它仍然会在生产中失败。整个方法都需要改变(可能任务被放弃了)。它只应该在本地工作。那你到底为什么要编写小程序呢?小程序只有在通过网络或internet部署给用户时才有意义。顺便说一句,这个小程序需要进行数字签名,普通应用程序不会。这在实时服务器上不起作用<代码>文件对象始终指向客户端文件系统。这在实时服务器上不起作用<代码>文件对象始终指向客户端文件系统。@AndrewThompson谢谢。我不知道。我投了赞成票。是否仍要列出服务器的文件?仅在服务器本身的帮助下。例如,某些服务器不安全,并且会在目录中提供HTML表或文件列表。小程序可能会解析它,但它很容易受到服务器更改的影响。或者,服务器端人员可以提供一个servlet(PHP、ASP等),小程序可以连接到该servlet,以获得一个更简单的列表。假定此服务器端功能将进行检查,以确保小程序仅查询特定允许目录的内容。
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"); } });
    }

}