-2

I am trying to read contents of a jar file. Below code i have written for this. I have placed the jar file under lib directory(\practice\lib\abc.jar).

final InputStream is = JarReader.class.getResourceAsStream("/abc.jar");
        BufferedReader input = new BufferedReader(new InputStreamReader(is));
        Scanner scan = new Scanner(input);
        while (scan.hasNext()) {
            String s = scan.next();
            System.out.println(s);
        }
        scan.close();

every time inputStream coming as null. Below post i have used for my reference getResourceAsStream returns null How to read a file from jar in Java?

TeamZ
  • 343
  • 6
  • 15
  • 1
    And so what ? Are you expecting us to write the code for you ? What have you tried ? What is not working as you expect ? – stellasia Jun 08 '15 at 15:21
  • i tried with below code final InputStream internalMetadataInputStream = this.getClass().getClassLoader() .getResourceAsStream(DirString); BufferedReader inp = new BufferedReader(new InputStreamReader(internalMetadataInputStream)); Scanner scan = new Scanner(inp); dl.debug("scan.hasNext()::"+scan.hasNext()); while (scan.hasNext()) { String s = scan.next(); } scan.close(); but it is pulling the line by line file content . – TeamZ Jun 08 '15 at 15:25
  • Please edit your question with this code (it would be easier to read). Thanks. – stellasia Jun 08 '15 at 15:29

1 Answers1

0

String s = scan.next();

This line in your code will read the file line after line. It's up to you on how to use those lines. You might want to create a StringBuilder and keep appending those lines. Or store it as plain String or something else if you prefer.

But you already have the file with you (in the form of individual lines).

Praba
  • 1,373
  • 10
  • 30
  • is there any other way i can get the file . I couldn't able to use the File api because it is searching the file from the own root directory . – TeamZ Jun 08 '15 at 15:35