Java Properties File not loading

后端 未结 2 1617
星月不相逢
星月不相逢 2020-12-22 10:47

I need a configuration file in my program to store some information, I saw some examples of properties files and was trying to use them but whenever I try this second line o

相关标签:
2条回答
  • 2020-12-22 11:33

    It looks like you do not have the fully qualified path to your properties file. You can get to it in 2 ways:-

    1. Using java.util.ResourceBundle:

    ResourceBundle bundle =ResourceBundle.getBundle("br.com.example.sortConfig"); //.properties is implied

    or

    1. Using ClassLoader.getResouceAsStream:
    sortConfig.load(this.getClass().getClassLoader().getResourceAsStream("br/com/example/sortConfig.propertie"));
    

    For a good tutorial on how to load properties files resources check out this link.

    0 讨论(0)
  • 2020-12-22 11:41

    An alternative could be to use this.getClass().getResourceAsStream() which accepts relative pathnames (relative to the package your class is in, that is), so you could simply write

    sortConfig.load(this.getClass().getResourceAsStream("sortConfig.properties"));
    

    This is useful when you specifically want to rely on your class and properties file being in the same package. (So when you move one during a refactoring, you'll have to move the other too.)

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