• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing file inside JAR

 
Chris Johnston
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am sure this question has been answered a million times, but I can not find it. I can only find things that are close but won't work. Here is my problem:

I have a jar file - foo.jar - that contains a main method. Inside this jar file I have a text file bar.txt. The layout is something like this:

What I need is to be able to gain access to bar.txt as a File. So far I have found the following method for accessing resources inside a jar file:



However, this won't work as I need to access the file using a character based stream and not a byte stream -- using


Ideally I need to access the file using a File object instead of a streamas I am using a component/method that takes a File as the input, not a InputStream.

[ March 02, 2005: Message edited by: Chris Johnston ]
[ March 02, 2005: Message edited by: Chris Johnston ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you absolutely must have a java.io.File, then your only choice would be to use getResourceAsStream() to suck the file's data out of the jar, write that data to a temporary file, and hand the File object for that temporary file off to your component.
 
Chris Johnston
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have got to be kidding, there is no easier way of doing this?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A File represents a path to a File in the filesystem. If you truly have to have a File, then, well, nope, that's it: you have to extract the data into a file.

Now, if what you really need is just to read the data with a Reader, then you can just say

BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(filename)));
 
Chris Johnston
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Won't that give me a Byte reader? What I need is a character/ascii reader. Is there any way to convert from a FileStreamReader to a FileReader, i.e., convert from a byte to an ascii stream?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no such thing as a "byte Reader". All Streams are byte-oriented, and all Readers and Writers are character oriented. The class InputStreamReader is an adapter between the two, which takes byte data and interprets it as (possibly multi-byte) characters. The class OutputStreamWriter goes in the opposite direction, turning characters into a raw byte stream. Once you've got an XXXReader, you've got character data.

You can't use FileReader here, because you don't have a file. But you can use BufferedReader -- or even InputStreamReader all by itself, if you don't need the buffering or the readLine() method that BufferedReader give you.
 
Chris Johnston
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I knew I was missing something here. In the back of my mind I knew there was something that could go between a an InputStream and an InputReader, I just could not remember what it was. You have cleared that up and jogged my memory as well. Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic