Search code examples
relative-pathabsolute-path

How to get a relative path from a given absolute path


How do I get out of the absolute path:

"C:/Users/test1234/workspace/Read/Test.txt"

the relative path? I thought the corresponding relative path looks like this:

".../Read/Test.txt"

but the file is not found, although the .txt file lies in the same directory as the java program. The file could be read with the absolute path, but I need the relative one. Any idea?


Solution

  • There is no such thing as the relative path from an absolute path. It all depends on your current location.

    But in your case I think you should replace the three dots by two dots. So make it:

    "../Read/Test.txt"
    

    (At least if your current location is C:/Users/test1234/workspace/Whatever/)