Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Using FileReader to read a file inside JAR

807605Jul 18 2007 — edited Jul 18 2007
I have the following code:

private void readFile(String type) {
BufferedReader in = null;
URL file = getClass().getResource("Hello.txt");
try {
in = new BufferedReader(new FileReader(file.getPath()));
} catch (FileNotFoundException ex) {
ex.printStackTrace();
System.out.println("Unable to open file.");
}
}

This works great within the compiler since the file is located within the pakage, however when I try to run the jar it does not work.

I need to figure out a way to keep the file location non-static and be able to read the file from within the JAR
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 15 2007
Added on Jul 18 2007
3 comments
1,486 views