aspose file tools*
The moose likes Java in General and the fly likes Accessing file inside JAR Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Accessing file inside JAR" Watch "Accessing file inside JAR" New topic
Author

Accessing file inside JAR

Chris Johnston
Ranch Hand

Joined: Dec 13, 2004
Posts: 85
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 ]

www.fuzzylizard.com
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Chris Johnston
Ranch Hand

Joined: Dec 13, 2004
Posts: 85
You have got to be kidding, there is no easier way of doing this?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

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

Joined: Dec 13, 2004
Posts: 85
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

Joined: Jul 08, 2003
Posts: 24168
    
  30

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

Joined: Dec 13, 2004
Posts: 85
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing file inside JAR
 
Similar Threads
Executable jar file problem
How to access a file from with a jar file?
Problem running jar
Ftp file to an AS400
Reading an xml file inside a Jar