【问题标题】:Check if folder exists using a numeric wildcard including decimals使用包括小数的数字通配符检查文件夹是否存在
【发布时间】:2016-03-16 14:07:24
【问题描述】:

我正在尝试编写代码来检查 Acrobat xx.x 文件夹是否存在于用户的 PC 上,其中xx.x 表示潜在的 Acrobat 版本号,例如 Acrobat 10.0 或 Acrobat 11.1。 意思是我不知道 xx.x 会是什么。找到here 的答案似乎假设我知道通配符的价值,但我不知道。

目的是最终将.js文件写入目录...\Program Files\Adobe\Acrobat xx.x\Acrobat\Javascripts

到目前为止,我确定是否安装了 32/64 位操作系统并将 Program Files 路径设置为 progFiles。假设安装了 Adob​​e,然后我需要使用此路径来确定 Acrobat xx.x 是否是子文件夹,例如:

folderToFind = new File(progFiles + "\\Adobe\\"+"\\Acrobat 11.1");

其中11.1 可以采用从 0.1 到 99.9 的任意数字的形式。然后我可以通过以下方式识别它的存在:

if (folderToFind.exists())
  {
    System.out.println("Acrobat folder found");
  }

一个明显的方法是创建一个循环,检查是否存在每一种可能性。这似乎是多余和不必要的。我希望有一种更有效的方法,例如:

  if (progFiles + "\\Adobe\\"+"\\Acrobat **.*\\Acrobat\\Javascripts".exists()){
 // ...
     }

有什么想法吗?谢谢

【问题讨论】:

  • 我已经读过,它似乎假设我知道XXX 将是什么......但就我而言,我正在寻找一个从 0.1 到 99.9 的动态数字。跨度>

标签: java directory wildcard exists


【解决方案1】:

你使用FileFilter

然后查找符合您描述的文件: Acrobat \d{1,2}\.\d

例子:

    File dir = new File(path);
    File[] matchingFiles = dir.listFiles(new FileFilter() {
        @Override
        public boolean accept(File pathname) {
            String regex = "Acrobat \\d{1,2}\\.\\d";
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(pathname.getName());
            return m.matches();
        }
    });
    for(File f : matchingFiles) {
        System.out.println(f.getName());
    }

您可以完全控制如何构建您的模式

【讨论】:

  • 这看起来很有希望,我会尽快测试并报告。谢谢
  • 完美运行,我一定会研究它。非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-22
  • 2011-02-12
  • 2013-02-08
  • 2022-11-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多