• Post Reply Bookmark Topic Watch Topic
  • New Topic

Relative path from the location of a class file

 
J Yau
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm looking for way to read a txt file which locates relatively to the location of the java class file.
e.g. .class file in ..../src/bin folder
.txt file in ..../src/param folder
I have tried to use the following
1) System.getProperty("user.dir")
it's just the working folder, not the folder where the class file locate.
2) ClassLoader.getSystemResource().getFile()
it return the full path of the class file but if the my class file inside "My Documents" folder, where the name of the folder contains space, the result will be sth like this "C:/My%20Document/abc.class"
Is there any other way to get the path of the class file.
And actually I would like to get the parent dir. of the class file then concatenate the relative path to the parent dir. for the target txt file.
Can any one help?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That "%20" is the hexadecimal code for a space, preceded by a "%" sign; that's a hint that the path has been "URL encoded". To decode a URL-encoded string just use the static method java.net.URLDecoder.decode().
I think I'd call this a Java bug, by the way... indeed, it is! Have a look at this entry in the Bug Database at the JDC:
http://developer.java.sun.com/developer/bugParade/bugs/4466485.html
The discussion is really interesting. Sun says this "won't be fixed" and yet there's a really outcry against it.
In any case, it looks as though what you really have to do is something like

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!