Dikla
Dikla

Reputation: 3491

getResourceAsStream not working for every class?

I have a jar file that uses some txt files. In order to get them it uses Class.getResourceAsStream function.

Class A
{
    public InputStream getInputStream(String path) throws Exception {
        try {
            return new FileInputStream(path);
        } catch (FileNotFoundException ex) {
            InputStream inputStream = getClass().getResourceAsStream(path);
            if (inputStream == null)
                throw new Exception("Failed to get input stream for file " + path);
            return inputStream;
        }
    }
}

This code is working perfectly.

Problem is, if I define class A as extends java.io.File, the InputStream I get from getResourceAsStream is null.

Also, if I leave class A as regular class (not inherited), and define class B as:

Class B extends java.io.File
{
    public InputStream getInputStream(String path) throws Exception
    {
     return new A().getInputStream(path);
 }
}

the returned InputStream is still null.

What is the problem? Is there a way to access the file from the class that inherits File?

Thanks,

Upvotes: 6

Views: 7665

Answers (4)

schmmd
schmmd

Reputation: 19448

Here are some examples that back up Jon Skeet's explanation:

Premise: You have a class my.package.A that depends on foo.txt.

  1. If you use my.package.A.class.getResourceAsStream("foo.txt") and you place foo.txt at my/package/foo.txt in your jar file, then when you run class A it should find foo.txt.

  2. if you use my.package.A.class.getClassLoader().getResourceAsStream("foo.txt") then place foo.txt in any folder on the classpath, when you run class A it should find foo.txt.

  3. if you use my.package.A.class.getResourceAsStream("/foo.txt") and you place foo.txt at /foo.txt in my jar file, then when you run project A it should find foo.txt.

Upvotes: 6

Grmpf
Grmpf

Reputation:

IMHO it's still better to use getClass().getClassLoader().getResourceAsStream() since the above code may fail if you subclass (and the subclass is in another pacakge) and call a parents method.

Upvotes: 1

Dikla
Dikla

Reputation: 3491

Sorry, the problem is different. (No problem actually...)

In the class that inherited from java.io.File I used getPath() in order to get the path to the resource. But the path had backslashes instead of regular slashes (since I work on Windows OS). When I used regular slashes, it worked even when using the inherited class.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499860

I suspect this is more to do with packages than inheritance.

If your class is in a package, then getResourceAsStream() will be relative to that package unless you start it with "/" (which makes it an "absolute" resource). An alternative is to use getClass().getClassLoader().getResourceAsStream() which doesn't have any idea of a path being relative to a package.

Just subclassing File shouldn't affect the behaviour at all. If you really believe it does, please post a short but complete program to demonstrate the problem.

Upvotes: 11

Related Questions