Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何创建从根目录到文件完整路径的映射_Java_File_Path - Fatal编程技术网

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

Java 如何创建从根目录到文件完整路径的映射,java,file,path,Java,File,Path,我正在尝试制作一种方法,比较一些根路径和文件完整路径,并将所有具有名称和每个目录完整路径的目录提取到映射 例如,让我们假设我想要制作一个映射,它看起来像这样: Map<String, File> mapFile = new HashMap<>; mapFile.put("root", new File("/root")); mapFile.put("dir1", new File("/root/dir1")); mapFile.put("dir2", new File("

我正在尝试制作一种方法,比较一些根路径和
文件
完整路径,并将所有具有名称和每个目录完整路径的目录提取到
映射

例如,让我们假设我想要制作一个
映射
,它看起来像这样:

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"));
Map-mapFile=新的HashMap;
put(“root”,新文件(“/root”);
put(“dir1”,新文件(“/root/dir1”);
put(“dir2”,新文件(“/root/dir1/dir2”);
put(“dir3”,新文件(“/root/dir1/dir2/dir3”);
以下是我迄今为止提出的解决方案:

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;
}
私有映射文件映射(字符串根路径,文件文件){
Map fileMap=newhashmap();
String path=file.getPath().substring(rootPath.length()).replaceAll(“\\\\\”,“/”;//fu windows。。。。
String[]chunks=path.split(“/”);
字符串p=rootPath.endsWith(“/”)?rootPath.substring(0,rootPath.length()-1):rootPath;
for(字符串块:块){
如果(chunk.isEmpty())继续;
p+=“/”+块;
put(块,新文件(p));
}
返回文件映射;
}
这就是应该如何使用:

Map<String, File> fileMap = fileMap("/root", new File("/root/dir1/dir2/dir3"));
fileMap.forEach((name, path) -> System.out.println(name + ", " + path));
Map fileMap=fileMap(“/root”,新文件(“/root/dir1/dir2/dir3”);
forEach((名称,路径)->System.out.println(名称+”,“+path));
主要的问题是我不喜欢它,它看起来就像是为了通过测试而做的……而且看起来很糟糕

Java中是否有任何内置的解决方案或功能可以使这一点更加清楚。编写这样的代码感觉就像我在试图找到如何煮开水。因此,任何帮助都将不胜感激。谢谢。

使用类获取目录名:

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;
}

在这种情况下,您根本不需要方法中的
File
,只需使用
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;
}
私有静态映射文件映射(字符串根路径,文件文件){
如果(!file.getAbsolutePath().startsWith(rootPath)){
抛出新的IllegalArgumentException(file.getAbsolutePath()+”不是“+rootPath”的子级);
}
文件根=新文件(根路径);
Map fileMap=newhashmap();
而(!root.equals(文件)){
fileMap.put(file.getName(),file);
file=file.getParentFile();
}
put(root.getName(),root);
返回文件映射;
}

谢谢。只有当根路径为“/”时才有空指针,但我想到了。这很容易出错。如果路径是
/root/foo/bar/foo/qux
,该怎么办?名称
foo
在其中有两次,但是
Map
不能有重复的键,因此不能存储
/root/foo
/root/foo/bar/foo
中的一个。你应该重新考虑你想做什么。是的,谢谢你提到这一点。我的最后一个想法是使用一个集合(列表)和一个存储目录名和完整路径的对象。我只是为了这个问题做了一个地图示例。
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;
}