aspose file tools
The moose likes Java in General and the fly likes Reading files from a directory by giving relative paths Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Reading files from a directory by giving relative paths" Watch "Reading files from a directory by giving relative paths" New topic
Author

Reading files from a directory by giving relative paths

omkar patkar
Ranch Hand

Joined: Aug 25, 2005
Posts: 231
Hello friends,

I want to read a file from a directory which is in parallel to the current directory.
For example, consider the following directory structure: -



In Tester.java i want to read the text.txt file which is actually in a directory parallel to the enclosing directory of Tester class.
I am trying to access the file by giving the path argument to the File class as


... but i get FileNotFoundException ... shouldn't the string argument that i provided go one level up( since i have specified two dots as we do while accessing directory in Linux) and then into the resources directory to access the test.txt ?

I want to access the the test.txt relative to the Tester.java ... because i can give the entire src folder to my friend ... he can put in any directory and try to run it without any change to the code!

How do i access the file relatively ? ... is my way of accessing the file relatively incorrect ?

P.S. By the way i am using Windows Vista Home Basic Edition OS and Java 1.6

Thanks and regards
Omkar Patkar


Thanks and Regards
Omkar Patkar (SCJP 1.4)[url]http://javacollectionsnotes.blogspot.com[/url] | [url]http://omkar-myscjpexp.blogspot.com[/url]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

The current folder (which the .. uses) is not Project\src\org\omkar\tester as you think, but the path where you were when you started the JVM. It's most likely Project\src, but you can always find out by printing the return value of System.getProperty("user.dir").

That said, try using resources instead of files. That way your code will work even when test.txt is packed inside the JAR file with the class files. Check out java.lang.Class#getResourceAsStream(java.lang.String) and java.lang.ClassLoader#getResourceAsStream(java.lang.String).


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
omkar patkar
Ranch Hand

Joined: Aug 25, 2005
Posts: 231
Thanks Rob!

I tried accessing the file by using the getResourceAsStream() API ... and it worked!

I had to specify the file as if it was only another java file with its package structure (... "dot" replaced with "forward slash")

I mean i accessed the file as "/org/omkar/resources/test.txt" ... now since my package structure is not going to change ... even if i give this to my friend,
the code should run on his machine as well!

Thanks a lot ! ... but i was wondering .... if the way by which i was trying earlier to give relative position of the file by using the syntax of double dots ".." and the file separator is right? .... does JVM understand this syntax that "..\" means go one level up of current directory ? .. or this syntax is understood only by Linux based OS ?

Thanks and Regards
Omkar Patkar
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Using relative paths works fine--as explained previously it's likely you're not starting the relative path from where you think you are.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Reading files from a directory by giving relative paths
 
Similar Threads
Load Property file in servlet or jsp
Unable to access a file in my Jar file.
Ant does not output property file in classes folder.
Escape sequences
Canonical vs Absolute