简体   繁体   English

Java解析具有不同工作目录的文件路径

[英]Java parse File-path with different working directory

I have a file which contain several paths, like . 我有一个包含几个路径的文件,例如. (relative) or /Users/...../ (absolut). (相对)或/ /Users/...../ (绝对)。 I need to parse the paths that are relative to the directory of the file that contains the paths and not the working-directory and create correct File-instances. 我需要解析相对于包含路径而不是工作目录的文件目录的路径,并创建正确的文件实例。 I can not change the working directory of the Java-Program, since this would alter the behaviour of other components and i also have to parse several files. 我无法更改Java程序的工作目录,因为这会改变其他组件的行为,并且我还必须解析几个文件。 I don't think public File(String parent, String child) does what i want, but i may be wrong. 我不认为public File(String parent, String child)可以满足我的要求,但是我可能是错的。 The documentation is quite confusing. 该文档非常混乱。

Example: 例:

file xy located under /system/exampleProgram/config.config has the following content:
.
/Users/Name/file
./extensions

i want to resolve these to:
/system/exampleProgram/
/Users/Name/file
/system/exampleProgram/file/

So, I am going to assume that you have access to the path of the file you opened (either via File.getAbsolutePath() if it was a File descriptor or via a regex or something)... 因此,我将假设您有权访问打开的文件的路径File.getAbsolutePath()如果是File描述符,则可以通过File.getAbsolutePath()或通过正则表达式或其他方式)...

Then to translate your relative paths into absolute paths, you can create new File descriptions with your opened file, like so: 然后,要将相对路径转换为绝对路径,可以使用打开的文件创建新的文件描述,如下所示:

File f = new File(myOpenedFilePath);
File g = new File(f, "./extensions");
String absolutePath = g.getCanonicalPath();

When you create a file with a File object and a String , Java treats the String as a path relative to the File given as a first argument. 当创建带有File对象和StringFile ,Java会将String视为相对于作为第一个参数的给定File的路径。 getCanonicalPath will get rid of all the redundant . getCanonicalPath将摆脱所有冗余. and .. and such. ..等等。

Edit: as Leander explained in the comments, the best way to determine whether the path is relative or not (and thus whether it should be transformed or not) is to use file.isAbsolute() . 编辑:正如Leander在评论中解释的那样,确定路径是否相对(以及是否应该转换)的最佳方法是使用file.isAbsolute()

Sounds like you probably want something like 听起来您可能想要类似


    File fileContainingPaths = new File(pathToFileContainingPaths);
    String directoryOfFileContainingPaths =
    fileContainingPaths.getCanonicalFile().getParent();
    BufferedReader r = new BufferedReader(new FileReader(fileContainingPaths));
    String path;
    while ((path = r.readLine()) != null) {
       if (path.startsWith(File.separator)) {
          System.out.println(path);
       } else {
          System.out.println(directoryOfFileContainingPaths + File.separator + path);
       }
    }       
    r.close();

Don't forget the getCanonicalFile() . 不要忘记getCanonicalFile() (You might also consider using getAbsoluteFile() ). (您也可以考虑使用getAbsoluteFile() )。

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

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