Search code examples
javaioinputstreambufferedreader

Reading a text file in my .jar


Lets say I'm making a program that needs to copy all the lines in a .txt file within my .jar file. it is in the package program.files and it is named text.txt. I've been looking all over the internet, and i cant find what I'm looking for. i think that this idea:

public String readSpecificFromJar(String dir, int line) {
    String read = null;
    try {
        InputStream in = getClass().getResourceAsStream(dir);
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(in));

        /**
         * declare string variable and prime the read
         */
        read = bufferedReader.readLine();

        for (int i = 1; i < line; i++) {
            read = bufferedReader.readLine();
        }

        bufferedReader.close();
    } catch (IOException ioexception) {
        Dialogs.fail("Could not read txt file from the JAR!!! Error Code: 06");
    }

    return read;
}

would work, but i tried that and it gave me all kinds of errors. what i think the problem would be is declaring the InputStream in the way it does it want the file to be right there with the Main method. how would i change this so it is not the case? thanks in advance!

EDIT: due to some confusion, i want to clear this up. for the String dir i am entering files/text.txt. it wont work. how do i fix this?

EDIT 2: OK i feel like the problem isn't getting across, and I'm kinda getting aggravated, mainly because I'm pretty tired. the code that WORKS for a different program is up above, where the dir is simply "text.txt"

THIS DOESN'T WORK FOR WHAT I'M DOING AND IM NOT SURE WHY. again, the file is IN THE CLASSPATH so dir is only "text.txt". I want my .txt file to be "files/text.txt". How do i do this?

EDIT 3: I dont know if i mentioned it, but my .txt file is INSIDE my jar. just to clear up the confusion. so really, the path of the .jar file shouldn't matter, as in I shouldn't have to type it in with the dir. also, the main class is in the package main and the .txt file is in the package files all within the same program named copy. also, i tried moving the txt file to the same package as the main class, also didn't work.

EDIT 4: by the way, the package that holds the method for reading from the jar is IO. as in the class io is inside the package IO. all of my files such as images and txt are in the package files. just thought id clear that up. i tried moving the txt file to the package of the io class, and that worked, but if its in any other package, even if i include the package name in the dir it wont work. any ideas as to why?


Solution

  • The .txt file should be in The root directory of dir is the same directory where your class file is located in.


    EDIT: due to some confusion, i want to clear this up. for the String dir i am entering files/text.txt. it wont work. how do i fix this?

    What is the path of the class? (the class that contains readSpecificFromJar()) If the .class file and the .txt file are both in the same directory, then you should make it like this:

    dir = "text.txt";
    

    EDIT 4: by the way, the package that holds the method for reading from the jar is IO. as in the class io is inside the package IO. all of my files such as images and txt are in the package files. just thought id clear that up. i tried moving the txt file to the package of the io class, and that worked, but if its in any other package, even if i include the package name in the dir it wont work. any ideas as to why?

    Try this:

    dir = "../files/text.txt";
    

    I believe this would work if your structure is as follows:

       javaApp.jar
            |
       _____|_____
      |           |
    files         IO
      |           |
      |           |
    text.txt   io.class