Confused about java properties file location

前端 未结 4 2109
走了就别回头了
走了就别回头了 2020-12-29 22:14

I have simple java project with structure:

package com.abc: a.java b.java c.properties

I have database configuration parameters conf

4条回答
  •  醉话见心
    2020-12-29 22:49

    First keep the default properties in your properties file, which gets packed into the jar. When the application starts try reading a same named properties file from some default location in filesystem, preferrable the user's home folder which you can obtain by System.getProperty("user.home");. If the file exists at the filesystem load it, if it doesn't exist then load your packed properties file and write a copy to the filesystem.

    So if your properties file name is myprops.properties, initially only your jar file will contain it. When the application starts up it will check whether /home/xyz/myprops.properties file exists. Since it doesn't, it will read the packed properties file and write a copy to /home/xyz/myprops.properties file. From next time onwards, it will read from /home/xyz/myprops.properties.

提交回复
热议问题