CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2001
    Location
    Trivandrum, Kerala, India
    Posts
    21

    Reading a text file from jar files


    In my application there are many class files. I have made a jar file from these class files. The jar file executes well. My program reads from a text file and displays its contents while running. I have also included this text file in the jar file. But my program is not reading the text file from the jar file.

    What should I do to read the text file from the jar file?

    Thanks in advance.

    Kishore.


  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Reading a text file from jar files

    You can use getResource(fileName) to create a URL for the file (this uses the ClassLoader which will search Jar files for resources), then get an InputStream from it, e.g. if file.txt is in a local Jar file you can read it like this:// Find file and make URL:
    URL url = getClass().getResource("file.txt");
    if (url != null) {
    try {
    // Read from URL input stream:
    InputStream ins = url.openStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(ins));
    String line = null;
    while ((line = br.readLine()) != null) {
    // do something with line;
    }
    }
    catch(IOException ioe) {
    System.out.println(ioe.getMessage());
    }
    }

    Alternatively, you could use the ZipFile class, but it's more effort...

    Dave


    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Jul 2001
    Location
    Trivandrum, Kerala, India
    Posts
    21

    Re: Reading a text file from jar files


    Thank you very much.

    Kishore.


Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured