• Post Reply Bookmark Topic Watch Topic
  • New Topic

The abstract path of IO  RSS feed

 
YanJun Tong
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an application whose package is as follows:
com.abc.bcd.efg

I wanna a file named FileTest in this package to approach another file called abc.txt under the f:/app/classes/com/abc/qwe/rty folder.

I have no idea how to access this file. my current approach is like
File f=new File("f:/app/classes/com/abc/qwe/rty/abc.txt");

Could anyone tell me how to change it into an abstract path like ../abc.txt.
Thanks
 
Petr Blahos
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi YanJun,

Have you try using the proper File("../../something/file.txt")?
It works for me perfectly. Anyway, this will stop working as soon
as you package your files in a jar file. You can access the files
regardless on if they are in filesystem or in jar using:

Class.getResourceAsStream("path/to/resource");

but your abc.txt would have to be in the same directory
as the class that accesses it, or in a subdirectory, i.e.
if your class is in:
com/company/project/sub1/MyClass1.class

it will be able to access:
com/company/project/sub1/resources/file.txt

but not
com/company/project/other/aaa.txt

Best regards,
Petr
 
Joe Ess
Bartender
Posts: 9439
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The path name can be relative, but it is relative from the current directory, not from the directory the class resides in. So if you were running from f:/app, your declaration would be:
File f=new File("classes/com/abc/qwe/rty/abc.txt");
The class Class has a particularly useful method called getResourceAsStream(), which finds a resource, like a text file or an image, in the classpath and returns an InputStream. In order to use this method, your file would need to be in a directory in the class path. In your case, probably f:/app/classes.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!