This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java Micro Edition and the fly likes Reading a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "Reading a file" Watch "Reading a file" New topic
Author

Reading a file

Sathya Srinivasan
Ranch Hand

Joined: Jan 29, 2002
Posts: 379
Hi,
Can someone tell me how to read a file that is packaged along with the jar file?
I read a post that files that are not packaged within the jar file cannot be read and that makes sense.
In my situation, I would like to read a bunch of strings from a file that I will supply with the jar file.
As an example, consider a quiz program for which I want to load the quiz questions and answers or a random quote generator program for which I would like to load the list of all quotes from which the program will select during load time.
Any help greatly appreciated.


Cheers, Sathya Srinivasan - SCJP 1.2, SCWCD 1.2, SCMAD 1.0
Co-Author of Whizlabs SCMAD Certification Exam Simulator and SCMAD Exam Guide Book
Pavlin Mihalev
Greenhorn

Joined: Apr 29, 2003
Posts: 28
Hello,
If I'm understanding right your question, here's what your solution will look like:
InputStream is = getClass().getResourceAsStream("/YOUR_FILENAME_HERE");
and then for example
DataInputStream dis = new DataInputStream(is);
I hope this helps ;-)
Cheers,
Pavlin


------<br />www.beiks.net
Sathya Srinivasan
Ranch Hand

Joined: Jan 29, 2002
Posts: 379
That worked perfectly fine. Thank you!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading a file
 
Similar Threads
using spring to develop java library, needed best practise for configuration?
Accessing Property-File outside of Jar?
Packaging an entire application in one JAR file
Writing back to JAR file?
load a jar file