2016-09-29 60 views
-1

我想在我的Java應用程序中顯示文件夾內的所有文件,如Windows資源管理器。如何在java中顯示文件夾內容/資源管理器

Windows explorer

我想創建一個GUI這樣的: enter image description here

那裏你可以看到,該路徑的所有文件夾和文件中列出。

有沒有人有一個很好的解決方案呢?

+1

又見[文件瀏覽器的GUI(http://codereview.stackexchange.com/q/4446/7784),其使用樹&表顯示文件。對於看起來更像上面的東西,將該表交換爲「JList」。 –

+0

*「很好的解決方案」*順便說一句:「良好的解決方案」是什麼意思?你需要知道使用什麼組件?你需要知道如何實現'後退'按鈕嗎?請具體說明,因爲*'我如何編寫該GUI?'對於SO來說太廣泛了。 –

+0

您可以使用Java中的樹或FileChooser。但是,如果你想要類似Windows資源管理器的東西,你可能必須自己創建它(這絕對是可行的)。首先,您必須知道如何首先從特定路徑讀取整個文件列表,然後將它們顯示在圖標中(您可以在其中創建自己的圖標類) – user3437460

回答

0

嘗試上市usibng從文件類的方法中的文件:

例子:

final File[] x = new File("C:\\").listFiles(); 
    for (final File file : x) { 
     System.err.println(file.getName()); 
     System.err.println(file.isDirectory()); 
     System.err.println(file.isFile()); 
    } 
0

不知道你想究竟是如何向他們展示,但這裏是一個辦法:

public void listFilesForFolder(final File folder) { 
    for (final File fileEntry : folder.listFiles()) { 
     if (fileEntry.isDirectory()) { 
      listFilesForFolder(fileEntry); 
     } else { 
      System.out.println(fileEntry.getName()); 
     } 
    } 
} 

final File folder = new File("/home/you/Desktop"); 
listFilesForFolder(folder); 

道具爲:Read all files in a folder

如果您正在構建GUI,我會建議使用文件選擇器的手勢: https://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html

0

爲此嘗試ApacheIO。用最少的代碼就可以實現它。

Class: FileUtils 
Method: 
     iterateFiles(File directory, String[] extensions, boolean recursive) 
     Allows iteration over the files in a given directory (and optionally its subdirectories) which match an array of extensions. 

     iterateFilesAndDirs(File directory, IOFileFilter fileFilter, IOFileFilter dirFilter) 
     Allows iteration over the files in given directory (and optionally its subdirectories). 

工作實施例:

http://www.programcreek.com/java-api-examples/index.php?class=org.apache.commons.io.FileUtils&method=listFilesAndDirs

相關問題