• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

Reading a file

 
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sathya Srinivasan
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That worked perfectly fine. Thank you!
 
Grow your own food... or this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic