Results 1 to 4 of 4
  1. #1
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default reading and writing to file within a jar archive

    Hi all

    I am trying to create a jar that reads from a text file within itself and I am really battling.

    I have an application that I know reads from these files just fine when it isn't all wrapped up as a jar and the minute I try to package it it won't read. Is this at all possible?

    The application will read the files when they are in in the same directory as the jar, but not when they are contained within the archive itself. :confused:

    Is it also possible to get a jar to write to a file contained within itself? I imagine this would involve having to unpack the archive, write to the file and repackage it. This I am less worried about as it is for logging and I really don't have an issue with the application writing logs to another file somewhere on the system.

  2. #2
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    When the file is in the jar, it's a resource, not a "file", per se.

    To read the resource, use the getResourceAsStream method (part of the Class class). This method returns an InputStream, which you can use to read the data, for example, a file.

    I'm not sure if you use Eclipse, but here are the steps I used.

    1) I created a new Source folder and called it "res" (for resource).
    2) I added a file, "input.txt", with two lines of text (for testing).

    Note that although the text file is in the "res" folder, you DON'T include "res" in the path. This is because all source folders are merged together, and the text file is in the "default package" (the root of the jar). To access the file, use URI notation, "/input.txt".

    The below code is an example that uses this process, it outputs the contents of the file.

    Java Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class ReadResource
    {
    	/**
    	 * Main function
    	 * 
    	 * @param args
    	 *            (not used)
    	 * @throws IOException
    	 */
    	public static void main(String[] args) throws IOException
    	{
    		BufferedReader in = new BufferedReader(new InputStreamReader(
    				ReadResource.class.getResourceAsStream("/input.txt")));
    		String line;
    
    		while ((line = in.readLine()) != null) {
    			System.out.println(line);
    		}
    
    		in.close();
    	}
    }
    Last edited by CodesAway; 10-10-2009 at 11:35 PM.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  3. #3
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default

    Quote Originally Posted by CodesAway View Post
    When the file is in the jar, it's a resource, not a "file", per se.

    To read the resource, use the getResourceAsStream method (part of the Class class). This method returns an InputStream, which you can use to read the data, for example, a file.

    I'm not sure if you use Eclipse, but here are the steps I used.

    1) I created a new Source folder and called it "res" (for resource).
    2) I added a file, "input.txt", with two lines of text (for testing).

    Note that although the text file is in the "res" folder, you DON'T include "res" in the path. This is because all source folders are merged together, and the text file is in the "default package" (the root of the jar). To access the file, use URI notation, "/input.txt".
    Firstly, thank you for the response.

    While this isn't exactly what I was hoping (I was hoping it wouldn't involve actually changing the code and that it was something that I was doing wrong when creating the jar file) it is definitely very helpful nonetheless.

    Why are they all in the default package?

    I thought a jar preserved the hierarchy because when I open it with an archive manager of some sort I can see all the directories. When a jar is created does it place all the resources (including the class files) into a single default package?

    Assuming I have that correct, how are the "package" details interpreted correctly in the classes if they are all now in the default package?

    Or have I misunderstood :confused:

  4. #4
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Quote Originally Posted by porchrat View Post
    Why are they all in the default package?

    I thought a jar preserved the hierarchy because when I open it with an archive manager of some sort I can see all the directories. When a jar is created does it place all the resources (including the class files) into a single default package?

    Assuming I have that correct, how are the "package" details interpreted correctly in the classes if they are all now in the default package?

    Or have I misunderstood :confused:
    Class files ARE placed in separate folders. Those in the "default package" would be in the root directory of the jar. For classes not in the default package, for example, "myPackage.subPackage.MyClass", change the "." to "/". For example, MyClass would be found at, "/myPackage/subPackage/MyClass.class".


    As for resources (like text files, icons, images, etc.), you can have them in a subfolder (steps to follow). In Eclipse, all the source folders are merged together. So, by placing input.txt in a source folder, even though it is named "res", it will be stored in the root of the jar.

    To place a resource in a subfolder, in Eclipse:

    1) I created a new Source folder and called it "res" (for resource).
    2) Then, in the "res" folder, add a new folder (just a normal folder, not a source folder) - I named my "res", for example.
    3) Add your files there.

    Now, since the resoures are in a subfolder, they will be located in a subfolder in the jar file. I usually just place my resources in the root (since I don't use the default package for classes i jar up).

    Now, to access the file, use the name of the subfolder ("res", in the above steps).

    Java Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class ReadResource
    {
    	/**
    	 * Main function
    	 * 
    	 * @param args
    	 *            (not used)
    	 * @throws IOException
    	 */
    	public static void main(String[] args) throws IOException
    	{
    		BufferedReader in = new BufferedReader(new InputStreamReader(
    				ReadResource.class.getResourceAsStream("/[COLOR="Red"]res/[/COLOR]input.txt")));
    		String line;
    
    		while ((line = in.readLine()) != null) {
    			System.out.println(line);
    		}
    
    		in.close();
    	}
    }
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

Similar Threads

  1. Replies: 3
    Last Post: 05-10-2009, 12:31 AM
  2. Reading and writing to a file
    By jigglywiggly in forum New To Java
    Replies: 13
    Last Post: 03-09-2009, 11:44 AM
  3. Reading/Writing to file
    By Doctor Cactus in forum New To Java
    Replies: 2
    Last Post: 10-28-2008, 03:05 PM
  4. writing/reading a Structure to/from a file
    By Farzaneh in forum New To Java
    Replies: 1
    Last Post: 08-31-2008, 03:21 PM
  5. Help with File reading and writing
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 07:47 PM

Posting Permissions

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