简体   繁体   English

如何创建从根目录到文件完整路径的映射

[英]How to create a Map from root directory to File full path

I am trying to make a method that compares some root path and a File full path and extract all directories with names and full paths to each directory into a Map我试图做出一些比较根路径和方法File的完整路径,并提取与名称和完整路径到每个目录下的所有目录到Map

For example lets say I want to make a Map that will looks something like this:例如,假设我想制作一个看起来像这样的Map

Map<String, File> mapFile = new HashMap<>;
mapFile.put("root", new File("/root"));
mapFile.put("dir1", new File("/root/dir1"));
mapFile.put("dir2", new File("/root/dir1/dir2"));
mapFile.put("dir3", new File("/root/dir1/dir2/dir3"));

Here is the solution that I came so far:这是我到目前为止的解决方案:

private Map<String, File> fileMap(String rootPath, File file) {
    Map<String, File> fileMap = new HashMap<>();
    String path = file.getPath().substring(rootPath.length()).replaceAll("\\\\", "/");// fu windows....
    String[] chunks = path.split("/");
    String p = rootPath.endsWith("/") ? rootPath.substring(0, rootPath.length() - 1) : rootPath;
    for (String chunk : chunks) {
        if (chunk.isEmpty()) continue;
        p += "/" + chunk;
        fileMap.put(chunk, new File(p));
    }
    return fileMap;
}

And that is how should be used:这就是应该如何使用:

Map<String, File> fileMap = fileMap("/root", new File("/root/dir1/dir2/dir3"));
fileMap.forEach((name, path) -> System.out.println(name + ", " + path));

The main problem is that I don't like it and it looks like it is made just to pass the tests...And it looks terrible.主要的问题是我不喜欢它,它看起来只是为了通过测试......而且它看起来很糟糕。

Is there any build in solutions or functionality in Java that can make this more clear. Java 中是否有任何内置的解决方案或功能可以使这一点更加清晰。 Coding something like this feels like I'm trying to find how to make a boil water.编写这样的代码感觉就像我在寻找如何煮沸水一样。 So any help would be appreciated.所以任何帮助将不胜感激。 Thanks.谢谢。

Use Path class for getting directory name:使用Path类获取目录名称:

private static Map<String, File> fileMap(String rootPath, File file) {
    Map<String, File> fileMap = new HashMap<>();
    fileMap.put(Paths.get(rootPath).getFileName().toString(), new File(rootPath));  // add root path
    Path path = file.toPath();

    while (!path.equals(Paths.get(rootPath))) {
        fileMap.put(path.getFileName().toString(), new File(path.toUri())); // add current dir
        path = path.getParent(); // go to parent dir
    }
    return fileMap;
}

You can even pass Path as an argument directly like您甚至可以直接将Path作为参数传递,例如

fileMap("/root", new File("/root/dir1/dir2/dir3").toPath());

In this case you don't need File in the method at all在这种情况下,您根本不需要方法中的File

You can just use the file.getParentFile() method to get down the file path until you get to the root directory:您可以使用file.getParentFile()方法来获取文件路径,直到到达根目录:

private static Map<String, File> fileMap(String rootPath, File file) {
    if (!file.getAbsolutePath().startsWith(rootPath)) {
        throw new IllegalArgumentException(file.getAbsolutePath() + " is not a child of " + rootPath);
    }
    File root = new File(rootPath);
    Map<String, File> fileMap = new HashMap<>();
    while (!root.equals(file)) {
        fileMap.put(file.getName(), file);
        file = file.getParentFile();
    }
    fileMap.put(root.getName(), root);
    return fileMap;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM