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());
}
}
代码
I want to list all the folders of the current directory but there are two problems. Everytime i run this program, the list shows all the files of the last folder inside the directory and the filter does not work.
Can you please help me?
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的方法不是递归的。目录是一棵树;您想要递归地调用一个方法,如果它是一个目录,则将文件列表添加到整个列表中,或者如果它是一个文件,则只需添加该文件。不断累积所有列表,您将在根目录下找到每个文件。
Your method isn't recursive. A directory is a tree; you want to recursively call a method that adds the list of files to an overall list if it's a directory, or simply add the file if it's a file. Keep accumulating all the lists and you'll have every file under the root directory.
FilenameFilter.accept
的第一个参数是包含目录。实际上,您的测试将始终返回true
。[编辑]
更具体地说,我认为你真的希望你的测试是:
The first parameter of
FilenameFilter.accept
is the containing directory. Effectively, your test will always returntrue
.[EDIT]
More specifically, I think you really want your test to be: