哪条相对路径是必要的

时间:2013-07-22 15:36:02

标签: java path

我的java文件例如有这样的路径com.company.project.util和文件javaClass.java。在这个文件中,我编写了一个程序来读取另一个文件,该文件位于文件夹META-INF/example.xml中。

此文件夹com.company.project.utilMETA-INF都位于同一文件夹src中。 我必须将哪个相对路径传递给这个javaClass.java来实现example.xml?有多少次需要使用../?因为我对这个大包名有点困惑。

3 个答案:

答案 0 :(得分:0)

如果从程序包根目录(src)运行程序并使用上述的FileReader,则只需使用“META-INF / example.xml”。如果工作目录不同,请根据需要修改此路径。

答案 1 :(得分:0)

只是举个例子

My Test1类在默认包中,我的资源文件夹在src文件夹中,我可以访问资源。你可以按照这个来映射你的需求。

公共类Test1 {

public static void main(String[] args) {
    new Test1();  
}
Test1(){
    BufferedInputStream file= (BufferedInputStream) this.getClass().getResourceAsStream("resources/a.txt");
    try {
        System.out.println((char)file.read());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

此外,如果您的java类在任何包中,那么您可以使用“../”作为每个文件夹的一步返回到类路径。一旦到达课程路径,就给出资源路径。

答案 2 :(得分:0)

如果文件位于源文件夹中,那么它就在您的类路径中,您将能够以绝对路径作为资源加载它:

this.getClass().getResourceAsStream("/META-INF/example.xml");
相关问题