2012-03-04 55 views
1

我試圖獲取目錄中的所有mp3文件的列表(在applet目錄中名爲music的子目錄),以便我可以在JavaScript函數中將它們的名稱推入數組。Java在目錄中列出文件

一切正常,但在上市過程......它只返回第一個MP3文件中的目錄,而不是其他的...

這是我的代碼

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() 
} 
+0

也許顯示addSong呢? – mplungjan 2012-03-04 09:13:15

+0

新增javascript – 2012-03-04 09:23:59

+3

您是否曾嘗試從另一個主機執行此applet,而不是提供此applet的主機?您無法使用'new File()'訪問服務器上的文件。 – 2012-03-04 09:43:37

回答

1

要列出(遞歸)所有文件(具有.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()); 
       } 
      } 
     } 
    } 
} 
+1

這不適用於實時服務器。 '文件'對象***總是***指向客戶端文件系統。 – 2012-03-04 09:50:34

+0

@AndrewThompson謝謝。這是我不知道的。我已經投了票。無論如何列出服務器的文件? – 2012-03-04 10:04:33

+0

謝謝小吃!它工作完美。 – 2012-03-04 10:06:44

0

我想你的代碼(基於Eclipse),它工作正常,要確保ü推杆的文件在正確的目錄

,我不能明白爲什麼ü在一個給定的目錄使用.substring(6)

+0

這不適用於實時服務器。 '文件'對象***總是***指向客戶端文件系統。 – 2012-03-04 09:50:24

1

我對你的代碼做了輕微的修改。一探究竟。這應該可以解決你的問題:

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

}