1

I have a setup in which I make use of a txt file (both reading and writing to it) in my program. At present I have it setup such that I use the local filepath on my machine, however I need to package it up into an executable JAR. To do this I've tried switching the filepath string over to the following:

String filepath = MyClass.class.getResource("/resources/textfile.txt");

However, when I run this I get a bunch of errors. After googling the method I found the similar method getResourceAsStream which I have also tried. This seems to return an InputStream object, however I need the filepath as a string ideally. Is this possible? If not what are my options?

Additional Info:

Here are the error messages I receive when trying to read & write to the txt file:

java.io.FileNotFoundException:/Users/Fred/Documents/Eclipse%20Projects/RandomProject/bin/resources/textfile.txt (No such file or directory)
1
  • In the future, please give details of errors rather than just saying "I get a bunch of errors". Please read tinyurl.com/so-hints Commented Mar 14, 2012 at 18:31

4 Answers 4

3

Well the code you've given won't compile, because Class.getResource returns a URL, not a String. You can't treat the resource as "just another file" - because it's not.

You should basically change whatever needs to read the file to accept an InputStream instead of a filename, and then pass in the result of calling getResourceAsStream().

Sign up to request clarification or add additional context in comments.

Comments

1

The method returns URL, not String. It's signature is public URL getResource(String name)

You might want to do:

String filepath = MyClass.class.getResource("/resources/textfile.txt").getPath();

1 Comment

Thanks for your comment, however when i try to use the filepath it throws up the java.io.FileNotFoundException despite the fact the filepath is 100% correct and I've checked it. Is there a reason why this would happen?
1

I have a setup in which I make use of a txt file (both reading and writing to it) in my program.

For read only, the resource can be in a Jar on the application class-path. It is very rare (in production) for resources on the application class-path to be writable. This text file will most probably need to be put in a reproducible path (e.g. a sub-directory of user.home - where the sub-dir is based on the package name) and used as a File from that path.

Or to put that a different way. I think you are pursuing the wrong path, to achieve the goal.

2 Comments

Good point: the original in the JAR should be cloned to produce a working copy in user space.
@trashgod Yeah, what you said - "cloned to produce a working copy in user space". I was trying to think of those words, but they eluded me. ;)
0

If you expect to directly write to a text file inside a JAR, my friend then you are wrong all the way! Please post some more code for us to understand what is it exactly you want to achieve and how you think it could be done.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.