在Java中从文件树中删除不需要的路径

时间:2018-01-24 08:25:22

标签: java

我已经实现了类似的东西

public class s {
    public static void main(String[] args) throws IOException {

        String rootDir = ".";
        String componentName="IAS/";
        String environmentName = "FAT/";
        String branchName = "FAT-TCP-IAS/";
        String instanceName = "IAS-jer-1";

        File folder = new File(rootDir + componentName);

        File source = new File(folder.toString());
        List<File> filesList = (List<File>) FileUtils.listFiles(source, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
        for (File file : filesList) {
            // work on the files
                System.out.println(file);
        }
    }

    }

此方法的结果是:

/IAS/default/1.3/pennytunnelservice.properties
/IAS/default/1.3/gateway.properties
/IAS/DEV/gateway.properties
/IAS/FAT/pennytunnelservice.properties
/IAS/FAT/FAT-TCP-IAS/IAS-jer-1/gateway.properties
/IAS/FAT/FAT-TCP-IAS/IAS-jer-1/version@1.5
/IAS/FAT/FAT-TCP-IAS/gateway.properties
/IAS/FAT/FAT-TCP-IAS/IAS-jer-2/version@1.9
/IAS/FAT/FAT-TCP-IAS/IAS-jer-2/gateway.properties
/IAS/FAT/FAT-SSL-IAS/gateway.properties
/IAS/FAT/gateway.properties

我希望得到的结果只是变量中没有提到的路径。所以我的预期结果应该是:

/IAS/default/1.3/pennytunnelservice.properties
/IAS/default/1.3/gateway.properties
/IAS/FAT/pennytunnelservice.properties
/IAS/FAT/FAT-TCP-IAS/IAS-jer-1/gateway.properties
/IAS/FAT/FAT-TCP-IAS/IAS-jer-1/version@1.5
/IAS/FAT/FAT-TCP-IAS/gateway.properties
/IAS/FAT/gateway.properties

只有带字符串的路径:

String componentName="IAS/";
String environmentName = "FAT/";
String branchName = "FAT-TCP-IAS/";
String instanceName = "IAS-jer-1";

请问你有什么暗示吗?

2 个答案:

答案 0 :(得分:0)

您必须将每个using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; namespace Test.Controllers { [Authorize(Roles = "Admin")] public class MoviesController : Controller { public IActionResult Index() { return View(); } } } 的路径与您的列表相匹配。
但是您必须重新考虑这些字符串,因为它们没有意义你给出的例子。

.c

谢谢,@ XtremeBaumer!

答案 1 :(得分:0)

如果要从目录打印输出中排除某些目录,则需要遍历路径的父目录,并检查是否所有路径都不在排除路径列表中。

例如:如果要从输出中排除它:

$ cat inputfile
>     This is First Line
>     ..
>     ..
>     ..
>     ..
>     This is Last Line, should not be deleted It could be come at any line

您需要遍历父路径:

  1. DEV
  2. IAS
  3. 看看它们是否都在排除列表中。例如,如果DEV位于排除列表中,则从输出中排除此文件。

    您可以使用与以下示例类似的代码来实现此排除行为。这里我们使用Java 7 /IAS/DEV/gateway.properties 类来遍历文件的路径(java.nio.file用于此目的)。

    file.getParent().spliterator()

    此代码的一个优点是您不需要外部库来递归遍历目录。这里使用的所有库都在JDK 8中。

    <强>更新

    如果您只想提及包含路径 - 颠倒上述逻辑 - ,您可以最低限度地更改上述代码以实现:

    List<String> exclusionPaths = Arrays.asList("elastic54", "elastic56", "elastic57", "elastic60");
    Files.walkFileTree(Paths.get("d:/work/eclipse"), new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            boolean printOut = StreamSupport.stream(file.getParent().spliterator(), false)
                    .noneMatch(path -> exclusionPaths.stream().anyMatch(s -> path.toString().equals(s)));
            if (attrs.isDirectory() || !printOut) {
                return FileVisitResult.CONTINUE;
            }
            System.out.println(file);
            return FileVisitResult.CONTINUE;
        }
    });
    

    您只需将List<String> inclusionPaths = Arrays.asList("elastic54", "elastic56", "elastic57", "elastic60"); Files.walkFileTree(Paths.get("d:/work/eclipse"), new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { boolean printOut = StreamSupport.stream(file.getParent().spliterator(), false) .anyMatch(path -> inclusionPaths.stream().anyMatch(s -> path.toString().equals(s))); if (attrs.isDirectory() || !printOut) { return FileVisitResult.CONTINUE; } System.out.println(file); return FileVisitResult.CONTINUE; } }); 方法更改为"noneMatch"