开发者

Create File object of file from parent directory in java

I have this issue of accessing a file in one of the parent directories.

To explain, consider the following dir structure:-

C:/Workspace/Appl/src/org/abc/bm/TestFile.xml  
C:/Workspace/Appl/src/org/abc/bm/tests/CheckTest.java

In the CheckTest.java I w开发者_Go百科ant to create a File instance for the TestFile.xml

public class Check {
    public void checkMethod() {
        File f = new File({filePath value I want to determine}, "TestFile.xml");
    }
}

I tried a few things with getAbsolutePath() and the getParent() etc but was getting a bit complicated and frankly I think I messed it up.

The reason I don't want to use "C:/Workspace/Appl/src/org/abc/bm" while creating the File instance is because the C:/Workspace/Appl is not fixed and in all circumstances will be different at runtime and basically I don't want to hard-code.

What could be the easiest and cleaner way to achieve this ?

Thank you.


You should load it from Classpath in this case.

In your CheckTest.java, try

FileInputStream fileIs = new FileInputStream(CheckTest.class.getClassLoader().getResourceAsStream("org/abc/bm/TestFile.xml");


Use System.getProperty to get the base dir or you set the base.dir during application launch

java -Dbase.dir=c:\User\pkg 

System.getProperty("base.dir");

and use

System.getProperty("file.separator");


What could be the easiest and cleaner way to achieve this ?

For accessing static resources use:

URL urlToResource = this.getClasS().getResource("path/to/the.resource");

If the resource is expected to change, write it to a sub-directory of user.home, where it is easy to locate later.


First of all, you can't get a reference to the source file path on runtime.

But, you can access the resrources included at your classpath (where you complied .class files will be).

Normally, your compiler will copy the xml file included at your srouce directory into the build directory, so at last, you could end up having something like this:

C:/Workspace/Appl/classes/org/abc/bm/TestFile.xml  
C:/Workspace/Appl/classes/org/abc/bm/tests/CheckTest.class

Then, with your classpath pointing to the compiled classes root dir, you get the resources from this directory, using the ClassLoader.getResource method (or the equivalent Class.getResource() method).

public class Check {
    public void checkMethod() {
        java.net.URL fileURL=this.getClass().getResource("/org/abc/bm/tests/TestFile.xml");

        File f=new File( fileURL.toURI());

    }
}


One could do this:

String pathOfTheCurrentClass = this.getClass().getResource(".").getPath();        
File file = new File(pathOfTheCurrentClass + "/..", "Testfile.xml");

or

String pathOfTheCurrentClass = this.getClass().getResource(".").getPath();
File filePath = new File(pathOfTheCurrentClass);
File file = new File(filePath.getParent(), "Testfile.xml");

But as Tomas Naros points out this gives you the file located in the build path.


Did you try

URL some=Test.class.getClass().getClassLoader().getResource("org/abc/bm/TestFile.xml");
File file = new File(some.getFile());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜