• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Reading a text file from a jar with an applet

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it possible read a file other than an image file from an applet's jar. I was thinking about sending some static data down in xml format
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure. getClass().getResourceAsStream() is your friend.
- Peter
 
George Thomas
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Peter, I'll give it a try.
George.
 
George Thomas
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again,
However...

I'm not sure why...
George.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure this is related, but perhaps it could give you a hint: when i've opened text-file resource's i've done it with an absolute resource path:
InputStream in = this.getClass().getResourceAsStream("/text.txt");
notice the /
// Jesper
 
Peter den Haan
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jesper is almost certainly right. Remember, in Java, a class's package is mapped to a filesystem path. When calling Class.getResourceAsStream(), you are asking for a resource in the same package as the class unless you prepend the resource name by a "/". For details, read the javadoc for this method. Note that the classloader itself does not have the concept of a current position in the package hierarchy, so ClassLoader.getResourceAsStream() always takes absolute paths.
Also, it is important to use a class that was loaded from the same location as your resource. If you are writing a web-application and you'd call getResourceAsStream() on a class that was loaded using the system classloader, for instance, it wouldn't be able to see your WEB-INF/classes or WEB-INF/lib directories.
- Peter
 
George Thomas
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes!
InputStream in = this.getClass().getResourceAsStream "/text.txt");
does work.
I'll go to the javadocs and see if I can get my mind clear on
what is happening here.
Thanks, George.
 
George Thomas
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes!
InputStream in = this.getClass().getResourceAsStream("/text.txt");
works!
I'll check out the javadocs as you suggested to see if I
can get clear in my mind what is happening here.
Thanks again,
George.
reply
    Bookmark Topic Watch Topic
  • New Topic