如何在 Java 中扫描文件夹?

发布于 2024-08-24 01:46:55 字数 127 浏览 2 评论 0原文

我需要在Java中扫描特定文件夹,并能够返回特定类型文件的整数个数(不仅基于扩展名,还基于命名约定。)例如,我想知道有多少个JPG文件\src 文件夹具有简单的整数文件名(例如 1.JPG 到 30.JPG)。有人能指出我正确的方向吗?谢谢

I need to scan a particular folder in Java, and be able to return the integer number of files of a particular type (based on not only extension but also naming convention.) For example, I want to know how many JPG files there are in the \src folder that have a simple integer filename (say, 1.JPG through 30.JPG). Can anyone point me in the right direction? Thx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

记忆里有你的影子 2024-08-31 01:46:55

java .io.File.list(FilenameFilter) 是您正在寻找的方法。

java.io.File.list(FilenameFilter) is the method you're looking for.

始于初秋 2024-08-31 01:46:55

我有一种方法,使用正则表达式模式来处理相当复杂的文件结构。可以使用类似的东西,尽管我确信它可以比我的示例编写得更简洁(为了安全性进行了编辑)。

/**
     * Get all non-directory filenames from a given foo/flat directory
     * 
     * @param network
     * @param typeRegex
     * @param locationRegex
     * @return
     */
    public List<String> getFilteredFilenames(String network, String typeRegex, String locationRegex) {

        String regex = null;

        List<String> filenames = new ArrayList<String>();
        String directory;

        // Look at the something network
        if (network.equalsIgnoreCase("foo")) {

            // Get the foo files first
            directory = this.pathname + "/" + "foo/filtered/flat";
            File[] foofiles = getFilenames(directory);

            // run the regex if need be.
            if (locationRegex != null && typeRegex != null ) {
                regex = typeRegex + "." + locationRegex;

                //System.out.println(regex);
            }


            for (int i = 0; i < foofiles.length; i++) {
                if (foofiles[i].isFile()) {
                    String file = foofiles[i].getName();

                    if (regex == null) {
                        filenames.add(file);
                    }
                    else {
                        if (file.matches(regex)) {
                            filenames.add(file);
                        }
                    }
                }
            }
        }

        return filenames;
    }

I have a method that uses a regex pattern for a rather complicated file structure. Something like that could be used, although I'm sure it could be written more concisely than my example (edited for security).

/**
     * Get all non-directory filenames from a given foo/flat directory
     * 
     * @param network
     * @param typeRegex
     * @param locationRegex
     * @return
     */
    public List<String> getFilteredFilenames(String network, String typeRegex, String locationRegex) {

        String regex = null;

        List<String> filenames = new ArrayList<String>();
        String directory;

        // Look at the something network
        if (network.equalsIgnoreCase("foo")) {

            // Get the foo files first
            directory = this.pathname + "/" + "foo/filtered/flat";
            File[] foofiles = getFilenames(directory);

            // run the regex if need be.
            if (locationRegex != null && typeRegex != null ) {
                regex = typeRegex + "." + locationRegex;

                //System.out.println(regex);
            }


            for (int i = 0; i < foofiles.length; i++) {
                if (foofiles[i].isFile()) {
                    String file = foofiles[i].getName();

                    if (regex == null) {
                        filenames.add(file);
                    }
                    else {
                        if (file.matches(regex)) {
                            filenames.add(file);
                        }
                    }
                }
            }
        }

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