简体   繁体   English

如何从文件系统中的相对路径中找到绝对路径

[英]How to find absolute path from a relative path in file system

A java class ReadFile1 in the TestA Project. TestA项目中的Java类ReadFile1。 And another class TestRead in project TestB. 在项目TestB中还有另一个类TestRead。 I want to read that class ReadFile1 in TestRead class. 我想在TestRead类中阅读该类ReadFile1。 I have the only relative path, then how can i read that. 我有唯一的相对路径,那我怎么读。

Example

My class is present in D:\\EIX-New-Source\\source\\message-kernel\\src\\main\\java\\com\\eix\\transform\\parser\\MessageTransformer.java 我的课程在D:\\ EIX-New-Source \\ source \\ message-kernel \\ src \\ main \\ java \\ com \\ eix \\ transform \\ parser \\ MessageTransformer.java中

message-kernel is the project in source folder. message-kernel是源文件夹中的项目。

and I am doing the test in D:\\EIX-New-Source\\source\\msg-Test\\src\\com\\coreJava\\rnd\\RelativePath.java 我在D:\\ EIX-New-Source \\ source \\ msg-Test \\ src \\ com \\ coreJava \\ rnd \\ RelativePath.java中进行测试

when I run this program then I got the result as D:\\EIX-New-Source\\source\\message-kernel\\target\\classes 当我运行该程序时,我得到的结果为D:\\ EIX-New-Source \\ source \\ message-kernel \\ target \\ classes

But I am expecting the result should be D:\\EIX-New-Source\\source\\message-kernel\\src\\main\\java\\com\\eix\\transform\\parser\\MessageTransformer.java 但是我希望结果应该是D:\\ EIX-New-Source \\ source \\ message-kernel \\ src \\ main \\ java \\ com \\ eix \\ transform \\ parser \\ MessageTransformer.java

Try the below method for getting the absolute path of your class file and then you can append the remaining path from relative file path to get the actual absolute path of your file. 尝试以下方法获取类文件的绝对路径,然后可以从相对文件路径追加剩余路径,以获取文件的实际绝对路径。

String getAbsolutePath(){
java.security.ProtectionDomain pd =
YourClassName.class.getProtectionDomain();
if ( pd == null ) return null;
java.security.CodeSource cs = pd.getCodeSource();
if ( cs == null ) return null;
java.net.URL url = cs.getLocation();
if ( url == null ) return null;
java.io.File f = new File( url.getFile() );
if (f == null) return null;

return f.getAbsolutePath();
}

You did not mention what you want to do after reading the file. 阅读文件后,您没有提及要执行的操作。 You can read the file by create a input stream. 您可以通过创建输入流来读取文件。 The input stream works for relative path also. 输入流也适用于相对路径。

FileInputStream inputStream = new FileInputStream(new File("../../com/eix/cromics/tdlr/ReadFile1"));

If you want to use it as java api then you need to define the dependency of project TestB on TestA. 如果要将其用作Java API,则需要定义TestB项目对TestA的依赖关系。

Firstly, Java save any file relative to the current working directory.If you give a package declaration then it will create the folder accordingly inside the current directory. 首先,Java保存相对于当前工作目录的任何文件,如果您提供了一个包声明,它将在当前目录中相应地创建该文件夹。
If you always want files to be saved relative to the location of the Java files and not the current working directory, you need to find that directory and pretend it to get an absolute path. 如果您始终希望相对于Java文件的位置而不是当前工作目录来保存文件,则需要查找该目录并假装它以获取绝对路径。

The working directory is where you run the program from ie where you call java TestClass, so when you want to write files relative to the class that's executing you'll have to know the current directory and append the relative path from there eg 工作目录是您从中运行程序的位置,即调用java TestClass的位置,因此,当您要编写相对于正在执行的类的文件时,您必须知道当前目录并从该目录追加相对路径,例如

public class TestClass {
  public void printFullPath() {
        String fullPath = String.format("%s/%s", System.getProperty("user.dir"),   this.getClass().getPackage().getName().replace(".", "/"));
        System.out.println(fullPath);
    }

}

For more details visit How to use relative path instead of absolute path? 有关更多详细信息,请访问如何使用相对路径而不是绝对路径?

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

相关问题 给定相对或绝对路径以及文件相对于的绝对路径时,如何获取文件的绝对路径 - How to get the absolute path of a file when given a relative or absolute path and the absolute path it is relative to 根据绝对路径查找相对路径 - Find relative path based on absolute path 如何从相对路径+类加载器获取绝对目录路径 - How to get absolute directory path from relative path + classloader 从绝对路径转换为相对路径 - Convert from absolute to relative path 这是相对路径还是绝对路径? - Is this a relative path or an absolute path? 如何使用相对路径而不是绝对路径? - How to use relative path instead of absolute path? AndroidStudio找不到CSV文件。 尝试了绝对文件路径和相对文件路径 - AndroidStudio can't find CSV file. Tried Absolute File Path & Relative File Path 从类路径,绝对路径,包括“ ../ ..”路径的相对路径中读取Java中的文件 - Read a file in Java from classpath, absolute path, relative path that includes “../..” path Hadoop从绝对路径和基本路径获取相对路径 - Hadoop get relative path from absolute path and base path 如何从可能表示绝对路径或相对路径的字符串创建文件? - How do I create a File from a string that may represent an absolute or a relative path?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM