0

I am trying to read text file whilst running the program from a jar archive. I come accros that I need to use InputStream to read file. The snippet of code:

buffer = new BufferedInputStream(this.getClass().getResourceAsStream((getClass().getClassLoader().getResource("English_names.txt").getPath())));


System.out.println(buffer.read()+" yeas");

At this line System.out.println(buffer.read()+" yeas"); program stops and nothing happens since then. Once you output the contents of buffer object it is not null. What might be the problem?

7
  • Your first line seems a little convoluted. Where lies "English_names.txt"? Commented Sep 9, 2012 at 21:47
  • At this stage it lies within project's directory. Commented Sep 9, 2012 at 21:48
  • Great news, I have tried FileInputStream to pass into InputStream, and it works fine now. Hopefully it works whilst you run the app within by using jar file. That was the aim of using InputStream Commented Sep 9, 2012 at 22:15
  • No. You should keep getResourceAsStream. Just use this.getClass().getResourceAsStream("English_names.txt") directly (if the file is in the base of your classpath. Commented Sep 9, 2012 at 22:19
  • "Within project directory" means the root of your source/class files (/src -> /bin or /classes) or its parent directory? Commented Sep 9, 2012 at 22:21

2 Answers 2

1

From InputStream#read():

This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

So basically, the stream appears to be waiting on content. I'm guessing it's how you've constructed the stream, you can simplify your construction to:

InputStream resourceStream = getClass().getResourceAsStream("/English_names.txt");
InputStream buffer = new BufferedInputStream(resourceStream);

I'd also check to make sure that resourceStream is not-null.

Sign up to request clarification or add additional context in comments.

6 Comments

You are right 'resourceStream' is null. But what is the problem, the file is in there...
@uml: in this case, getResourceAsStream is used to get files that are on the classpath. If you want to open a file not on the classpath, perhaps you should check out FileInputStream.
/D:/Aristotelis/J-A-V-A/Msc_project/code/branches/Crypto/bin/English_names.txt Tha is the path returned by some method. Why is there '/' in front of D partition? Is that okay?
@uml: cool, try adding a slash (modified my answer): getClass().getResourceAsStream("/English_names.txt");
No, it does not help. The file is within project's directory. That should mean it is on the classpath.
|
0

You should not worry about InputStream being null when passed into BufferedInputStream constructor since it, the constructor handles null parameters just fine. When supplied with null it will just return null without throwing any exception. Also since InputStream implements AutoClosable the try-with-resources block will take care of closing your streams properly.

try (
        final InputStream is = getClass().getResourceAsStream("/English_names.txt");
        final BufferedInputStream bis = new BufferedInputStream(is);
        ) {
        if (null == bis)
            throw new IOException("requsted resource was not found");
        // Do your reading. 
        // Do note that if you are using InputStream.read() you may want to call it in a loop until it returns -1
    } catch (IOException ex) {
        // Either resource is not found or other I/O error occurred 
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.