How do I use the Java ClassLoader to load a file fromthe classpath?

前端 未结 3 2116
自闭症患者
自闭症患者 2020-12-31 18:34

I want to use the ClassLoader to load a properties file for the Properties class. I\'ve simplified the below code to remove error handling for the purposes of this discussio

相关标签:
3条回答
  • 2020-12-31 18:48

    If the file is in the same directory as the class, you have to prefix the class's package as a directory.

    So if your package is:

    package com.foo.bar;
    

    Then your code is:

    .getResourceAsStream("com/foo/bar/theta.properties");
    
    0 讨论(0)
  • 2020-12-31 19:03

    You can use ResourceBundle

    0 讨论(0)
  • 2020-12-31 19:13

    By using getClass().getClassloader() you look for "theta.properties" from the root path directory. Just use getClass().getResourceAsStream() to get a resource relative to that class.

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