How to add a Java Properties file to my Java Project in Eclipse

前端 未结 8 889
[愿得一人]
[愿得一人] 2020-12-15 20:22

I was using Unix before to compile and edit my Java. In that I have used property files right inside my current working directory where the class file exists. Now i have swi

相关标签:
8条回答
  • 2020-12-15 20:55
    1. Right click on the folder within your project in eclipse where you want to create property file
    2. New->Other->in the search filter type file and in the consecutive window give the name of the file with .properties extension
    0 讨论(0)
  • 2020-12-15 20:57
    1. Create Folder “resources” under Java Resources folder if your project doesn’t have it.
      1. create config.properties file with below value.

    /Java Resources/resources/config.properties

    for loading properties.

    Properties prop = new Properties();
    InputStream input = null;
    
    try {
    
          input = getClass().getClassLoader().getResourceAsStream("config.properties");
    
    
        // load a properties file
        prop.load(input);
    
        // get the property value and print it out
        System.out.println(prop.getProperty("database"));
        System.out.println(prop.getProperty("dbuser"));
        System.out.println(prop.getProperty("dbpassword"));
    
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题