5 Replies - 2897 Views - Last Post: 21 September 2011 - 06:10 AM Rate Topic: -----

#1 Greltam  Icon User is offline

  • D.I.C Head

Reputation: 90
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Jar class.getResource() to text file difficulty

Posted 20 September 2011 - 05:14 PM

Hello, I'm trying to solve a seemingly tricky problem. I'm trying to load a txt file that is packaged inside the jar. Now I've only had experience loading images via getResourceAsStream, and I can read the file using a Scanner with getResourceAsStream, but I'm wondering if there is a way to get just a File.

I've tried doing it this way:
File f = new File(this.getClass().getResource("/images/IntroScreen/logo.txt").toURI());
and
File f = new File(this.getClass().getResource("/images/IntroScreen/logo.txt").getFile());



However, I get the error
java.io.FileNotFoundException: file:\C:\Users\Grexel\workspace\RunKnight.jar!\images\IntroScreen\logo.txt 
(The filename, directory name, or volume label syntax is incorrect)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.util.Scanner.<init>(Unknown Source)
    at SpriteSheet.loadSpriteSheet(SpriteSheet.java:112)
    etc. etc. etc


Should I ditch trying to load the file and focus on loading the stream? Or is there a way to do this that I haven't thought of/found yet?

Is This A Good Question/Topic? 0
  • +

Replies To: Jar class.getResource() to text file difficulty

#2 supercorey  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 119
  • View blog
  • Posts: 207
  • Joined: 15-February 09

Re: Jar class.getResource() to text file difficulty

Posted 20 September 2011 - 05:30 PM

I'm sure there probably is a way to make your method work, but I had written some code that retrieves a file from a JAR already so I'll just post something that I know functions.

        InputStream is = null;
        BufferedReader br = null;
        
        try { 
            is = FileUtils.class.getResourceAsStream(s);
            br = new BufferedReader(new InputStreamReader(is));
            //do whatever you want with the stream
        }catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) br.close();
                if (is != null) is.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }


Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Jar class.getResource() to text file difficulty

Posted 20 September 2011 - 05:45 PM

If the /images directory is under your .class location should be
"images/IntroScreen/logo.txt"
not
"/images/IntroScreen/logo.txt"
Was This Post Helpful? 0
  • +
  • -

#4 Greltam  Icon User is offline

  • D.I.C Head

Reputation: 90
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: Jar class.getResource() to text file difficulty

Posted 20 September 2011 - 07:10 PM

Either way it does the same. I tried without the leading / first, but somewhere someone suggested adding it. I also tried .getContent() on the URL returned, with gave a funny object back (sun.net.www.content.text.PlainTextInputStream).
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Jar class.getResource() to text file difficulty

Posted 20 September 2011 - 07:37 PM

Not sure neither of your two trials to read from a .jar will work
File f = new File(this.getClass().getResource("/images/IntroScreen/logo.txt").toURI());
and
File f = new File(this.getClass().getResource("/images/IntroScreen/logo.txt").getFile());


A file is nn abstract representation of file and directory pathnames. I doubt it can contain a .jar as the error reported shows:

\C:\Users\Grexel\workspace\RunKnight.jar!\images\IntroScreen\logo.txt

this is surely not a valid file path syntax. Can you image:
C:\users\me\abc.zip!\root\foo.txt ?


The documented way that I have always seen is supercorey's one:

InputStream is = FileUtils.class.getResourceAsStream(s);
BufferedReader br = new BufferedReader(new InputStreamReader(is));

This post has been edited by pbl: 20 September 2011 - 07:39 PM

Was This Post Helpful? 1
  • +
  • -

#6 Greltam  Icon User is offline

  • D.I.C Head

Reputation: 90
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: Jar class.getResource() to text file difficulty

Posted 21 September 2011 - 06:10 AM

Ok. Sounds legit.2
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1