aspose file tools*
The moose likes Java in General and the fly likes Reading from a file located in same folder as my JAR file. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reading from a file located in same folder as my JAR file." Watch "Reading from a file located in same folder as my JAR file." New topic
Author

Reading from a file located in same folder as my JAR file.

Krishna Komarpant
Greenhorn

Joined: Aug 07, 2009
Posts: 15
Hi All,

I have my Netbeans Project after building of which creates a JAR file in the "dist" folder.
Now i want to read some settings stored in a "settings.ini" which i pasted in the same directory as the JAR file.

In my code i give directly, the name of the file to read from in the File Input stream. This does not work, and gives error saying cannot read from file, there is a path resolvnig problem...

What is the correct code notation to read from the file?

Following is the code:
r035198x
Greenhorn

Joined: Sep 13, 2006
Posts: 3
On File(Settings.ini), did you mean

instead?

Krishna Komarpant
Greenhorn

Joined: Aug 07, 2009
Posts: 15
r035198x wrote:On File(Settings.ini), did you mean

instead?



Oh yes... I apologize for that. It was a typo, i was in haste so did not realize that mistake.

By the way, i figured it out..
I kept the "Settings.ini" file in the Project Home folder and gavethe same format statement in the code

and continued with my file reading code, and it works perfect.

Thanks anyways!
Ganesh Gowtham
Ranch Hand

Joined: Mar 30, 2005
Posts: 225

Hi Krishna

Could you please try this solution




If you ReadFromFile is packed as jar in name myJar.jar and place Settings.ini in same folder

Above code will read properties file "Settings.ini" .


Thanks, Ganesh Gowtham
http://ganesh.gowtham.googlepages.com
Krishna Komarpant
Greenhorn

Joined: Aug 07, 2009
Posts: 15
Yes this will work, bu i realised that i was making my code to commit suicide..

When you write the above code, it takes the base directory for the "Settings.ini" file to be in "projectHomeFolder/build/" directory, which is always deleted and re-created when you build you whole project.

As i have mentioned in the previous post, the solution was to paste the "Settings.ini" file in the "ProjectHomeFolder"

Zandis Murāns
Ranch Hand

Joined: Aug 18, 2009
Posts: 174

getResourceAsStream()

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading from a file located in same folder as my JAR file.