Saturday, February 5, 2011

Directory path for Java applications

It happens that I would like to load or store files with a Java application. Back in the old days, I always wondered how to construct the right relative path. The clue is that all relative pathes are being evaluated from the point of the root directory of the Java application. Considering the whole project as one simple exe file may help to understand this. The basic directions therefore are
  • moving down a directory

    [subdirectory/]subdirectory/file.file-extension

    which is equal to

    ./subdirectory(/subdirectory)file.file-extension
  • moving up a directory

    [../]../file.file-extension

To access a file in a directory on the same level of the application directory, you therefore need to go up a directory to move down into the target directory afterwards.
../target-directory/file.file-extension