Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8614287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:05:12+00:00 2026-06-12T05:05:12+00:00

Lets say I’m making a program that needs to copy all the lines in

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T05:05:14+00:00Added an answer on June 12, 2026 at 5:05 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

lets say i have an xml file path.xml that goes like this: <paths> <path
Lets say I have a class A that is fairly simple like this -
Lets say that i have 2 pages: index.php and service.php index.php sends an http-post
Lets say that I have 4 view controllers (call them FirstView,SecondView,ThirdView,FourthView) which are created
Lets say that I have this code $(document).ready(function() { $(#changeText).click(function() { $(#textBox).html(My<br><br><br>text<br><br><br>is<br><br><br>changed!); }); });
Lets say that i have 2 instances of the same entity, a and b
Lets say, for example, that I have a class that requires the use of
Lets say I have a .c file with some methods, and I create a
Lets say in a file I have some code in two files part of
Lets say that I have gps coordinates for 1000 stores and a short text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.