Java目录列表问题

发布于 2024-11-07 13:13:14 字数 1620 浏览 2 评论 0原文

我想列出当前目录的所有文件夹,但有两个问题。每次我运行这个程序时,列表都会显示目录中最后一个文件夹的所有文件,并且过滤器不起作用。

你能帮我吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失而复得 2024-11-14 13:13:14

你的方法不是递归的。目录是一棵树;您想要递归地调用一个方法,如果它是一个目录,则将文件列表添加到整个列表中,或者如果它是一个文件,则只需添加该文件。不断累积所有列表,您将在根目录下找到每个文件。

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.

情绪操控生活 2024-11-14 13:13:14

FilenameFilter.accept 的第一个参数是包含目录。实际上,您的测试将始终返回 true

[编辑]

更具体地说,我认为你真的希望你的测试是:

public boolean accept(File dir, String fileName) {
    return new File(dir, fileName).isDirectory();
}

The first parameter of FilenameFilter.accept is the containing directory. Effectively, your test will always return true.

[EDIT]

More specifically, I think you really want your test to be:

public boolean accept(File dir, String fileName) {
    return new File(dir, fileName).isDirectory();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文