【问题标题】:Reading a file with regex in the path of the file在文件路径中使用正则表达式读取文件
【发布时间】:2018-03-16 19:32:36
【问题描述】:

我想阅读这个 JSON 文件 - '/Users/test/dos-2018-03-02-22-23/report.json'。但是我在阅读时不知道确切的时间戳,因此在阅读时我需要一个 dos-* 文件夹的正则表达式。

要读取文件,我正在这样做:

JSONObject jsonObject = (JSONObject) parser.parse(new FileReader(FILE_PATH));

现在文件夹路径不断变化[假设只有一个],但我知道它以“dos-”开头。那么如何在路径中使用这个正则表达式模式呢?

我尝试过使用路径匹配器,但它可以用来匹配我猜到的路径。

dosReportFilePath = "/Users/test/";
dosReportFileName = "/report.json"
PathMatcher pathMatcher = fileSystem.getPathMatcher("glob:"+dosReportFilePath+"/dos-*/"+dosReportFileName);

我不确定如何在 FileReader 中使用它。任何有助于实现这一目标的帮助将不胜感激。

找到一个解决方案:

我可以读取 dosReportFilePath 中的所有目录以获取以“dos-”开头的文件夹名称。[Java, List only subdirectories from a directory, not files

【问题讨论】:

标签: java json regex filereader


【解决方案1】:

我使用这个Java, List only subdirectories from a directory, not files 来获取所有目录并找到以“dos-”开头的目录。谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-03
    • 1970-01-01
    • 2014-08-03
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    • 1970-01-01
    相关资源
    最近更新 更多