我想列出當前目錄的所有文件夾,但有兩個問題。每次我運行這個程序時,列表顯示目錄中最後一個文件夾的所有文件,並且過濾器不起作用。Java目錄列表問題
你能幫我嗎?
public void foldersPanel()
{
JPanel folderScreen = new JPanel();
folderScreen.setLayout(new GridLayout(1,1));
direFilter = new FilenameFilter()
{
public boolean accept(File file, String string)
{
if (file.isDirectory())
{
return file.isDirectory();
}
else
{
return false;
}
}
};
File directory = new File(System.getProperty("user.dir"));
File[] listOfFiles = directory.listFiles(direFilter);
String[] allFiles = directory.list();
if (directory.isDirectory())
{
int x = 1;
for (int i =0; i<listOfFiles.length; i++) // displays all the files
{
if (listOfFiles[i].isDirectory())
{
folderList = new JList(listOfFiles[i].list(direFilter));
System.out.println(x +") Directory = " + listOfFiles[i].getName());
x++;
folderList.setSelectedIndex(1);
}
else
{
System.out.println("is file");
}
folderScreen.add(new JScrollPane(folderList));
}
}
else
{
System.out.println("This is a file, not a directory, so we will move to the parent folder");
directory = directory.getParentFile();
updateLabels();
System.out.println("The new directory is : " +directory.toString());
}
}
code
你得到具體什麼錯誤? 「目錄中的最後一個文件夾」是什麼意思?你能否澄清你的問題?當前目錄中的 – alpian 2011-05-17 23:22:24
是3個文件夾,文件夾1,文件夾2和文件夾3.每次運行此代碼時,目錄的位置是當前目錄/ folder3 – Eristikos 2011-05-19 16:31:24