Different ways of loading a file as an InputStream

前端 未结 6 604
予麋鹿
予麋鹿 2020-11-22 04:02

What\'s the difference between:

InputStream is = this.getClass().getClassLoader().getResourceAsStream(fileName)

and

InputSt         


        
相关标签:
6条回答
  • 2020-11-22 04:14

    It Works , try out this :

    InputStream in_s1 =   TopBrandData.class.getResourceAsStream("/assets/TopBrands.xml");
    
    0 讨论(0)
  • 2020-11-22 04:22

    All these answers around here, as well as the answers in this question, suggest that loading absolute URLs, like "/foo/bar.properties" treated the same by class.getResourceAsStream(String) and class.getClassLoader().getResourceAsStream(String). This is NOT the case, at least not in my Tomcat configuration/version (currently 7.0.40).

    MyClass.class.getResourceAsStream("/foo/bar.properties"); // works!  
    MyClass.class.getClassLoader().getResourceAsStream("/foo/bar.properties"); // does NOT work!
    

    Sorry, I have absolutely no satisfying explanation, but I guess that tomcat does dirty tricks and his black magic with the classloaders and cause the difference. I always used class.getResourceAsStream(String) in the past and haven't had any problems.

    PS: I also posted this over here

    0 讨论(0)
  • 2020-11-22 04:24

    After trying some ways to load the file with no success, I remembered I could use FileInputStream, which worked perfectly.

    InputStream is = new FileInputStream("file.txt");
    

    This is another way to read a file into an InputStream, it reads the file from the currently running folder.

    0 讨论(0)
  • 2020-11-22 04:29

    Use MyClass.class.getClassLoader().getResourceAsStream(path) to load resource associated with your code. Use MyClass.class.getResourceAsStream(path) as a shortcut, and for resources packaged within your class' package.

    Use Thread.currentThread().getContextClassLoader().getResourceAsStream(path) to get resources that are part of client code, not tightly bounds to the calling code. You should be careful with this as the thread context class loader could be pointing at anything.

    0 讨论(0)
  • 2020-11-22 04:31

    Plain old Java on plain old Java 7 and no other dependencies demonstrates the difference...

    I put file.txt in c:\temp\ and I put c:\temp\ on the classpath.

    There is only one case where there is a difference between the two call.

    class J {
    
     public static void main(String[] a) {
        // as "absolute"
    
        // ok   
        System.err.println(J.class.getResourceAsStream("/file.txt") != null); 
    
        // pop            
        System.err.println(J.class.getClassLoader().getResourceAsStream("/file.txt") != null); 
    
        // as relative
    
        // ok
        System.err.println(J.class.getResourceAsStream("./file.txt") != null); 
    
        // ok
        System.err.println(J.class.getClassLoader().getResourceAsStream("./file.txt") != null); 
    
        // no path
    
        // ok
        System.err.println(J.class.getResourceAsStream("file.txt") != null); 
    
       // ok
       System.err.println(J.class.getClassLoader().getResourceAsStream("file.txt") != null); 
      }
    }
    
    0 讨论(0)
  • 2020-11-22 04:33

    There are subtle differences as to how the fileName you are passing is interpreted. Basically, you have 2 different methods: ClassLoader.getResourceAsStream() and Class.getResourceAsStream(). These two methods will locate the resource differently.

    In Class.getResourceAsStream(path), the path is interpreted as a path local to the package of the class you are calling it from. For example calling, String.class.getResourceAsStream("myfile.txt") will look for a file in your classpath at the following location: "java/lang/myfile.txt". If your path starts with a /, then it will be considered an absolute path, and will start searching from the root of the classpath. So calling String.class.getResourceAsStream("/myfile.txt") will look at the following location in your class path ./myfile.txt.

    ClassLoader.getResourceAsStream(path) will consider all paths to be absolute paths. So calling String.class.getClassLoader().getResourceAsStream("myfile.txt") and String.class.getClassLoader().getResourceAsStream("/myfile.txt") will both look for a file in your classpath at the following location: ./myfile.txt.

    Everytime I mention a location in this post, it could be a location in your filesystem itself, or inside the corresponding jar file, depending on the Class and/or ClassLoader you are loading the resource from.

    In your case, you are loading the class from an Application Server, so your should use Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName) instead of this.getClass().getClassLoader().getResourceAsStream(fileName). this.getClass().getResourceAsStream() will also work.

    Read this article for more detailed information about that particular problem.


    Warning for users of Tomcat 7 and below

    One of the answers to this question states that my explanation seems to be incorrect for Tomcat 7. I've tried to look around to see why that would be the case.

    So I've looked at the source code of Tomcat's WebAppClassLoader for several versions of Tomcat. The implementation of findResource(String name) (which is utimately responsible for producing the URL to the requested resource) is virtually identical in Tomcat 6 and Tomcat 7, but is different in Tomcat 8.

    In versions 6 and 7, the implementation does not attempt to normalize the resource name. This means that in these versions, classLoader.getResourceAsStream("/resource.txt") may not produce the same result as classLoader.getResourceAsStream("resource.txt") event though it should (since that what the Javadoc specifies). [source code]

    In version 8 though, the resource name is normalized to guarantee that the absolute version of the resource name is the one that is used. Therefore, in Tomcat 8, the two calls described above should always return the same result. [source code]

    As a result, you have to be extra careful when using ClassLoader.getResourceAsStream() or Class.getResourceAsStream() on Tomcat versions earlier than 8. And you must also keep in mind that class.getResourceAsStream("/resource.txt") actually calls classLoader.getResourceAsStream("resource.txt") (the leading / is stripped).

    0 讨论(0)
提交回复
热议问题