Results 1 to 3 of 3

Thread: Java problem (reading text from jar file)

  1. #1
    fernandez's Avatar

    BBS Klingon

    Join Date
    Jan 2003
    Posts
    298

    Java problem (reading text from jar file)

    My program is supposed to read a text file contained withing the jar file which encapsulates all classes, images and text files. The problem occurs when the program tries to read the text file inside the jar file.
    I have a BufferedReader that's supposed to get the contents of the text, but I keep getting an error (java.io.FileNotFoundException). How do I specify the path so that the program will fetch the text file from the jar file?

    Thanks,

    fernandez

  2. #2
    fernandez's Avatar

    BBS Klingon

    Join Date
    Jan 2003
    Posts
    298
    Nevermind, it's working now. You have to use an InputStream and call the getResourceAsStream method. I was using a regular FileReader to try to access the file.

  3. #3

    Zeropaid Noob

    Join Date
    Jun 2007
    Posts
    1

    Thumbs up clarification

    fernandez answers his own question, but I clarify both the question and the answer here.

    The question is: how do you access a file that is inside the jarfile you have deployed your app or applet in? Using a FileReader will start searching the local directory where the app was launched, and will never look inside the jar file to find anything.

    The answer is: Use the Class class, and call the getResourceAsStream() method. This has all the paraphernalia of the class loading system (which is already active in launching your app) available to it. See http://mindprod.com/jgloss/getresourceasstream.html
    And if you get muddled, try starting your resource filename with a /.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •