简体   繁体   中英

Folder Path in .properties file

I was trying to put all the paths(folder paths/urls) in a .properties file, so that if I'm going to change my paths it will be easier. So this is one of the data in my path.properties:

path.pvInfoIni=C:\\Palmus-HACMS\\PV\\PvInfo.ini

and this is the real directory of the file:

C:\\Palmus-HACMS\\PV\\PVInfo.ini

this path leads to an .ini file where data are being written.

Here's the code where I call the path:

Properties property;
FileInputStream fs;     
fs = new FileInputStream(System.getProperty("user.dir")+"\\path.properties");


common.writeIniFileIdentify("PV-ID", PVIDNo); // PsFileAccessorIni.GetInstance().GetValueString(PsFileAccessorIni.PVIDLocation));
common.writeIniFileIdentify("PALMUS-ID", SerialNo);// PsFileAccessorIni.GetInstance().GetValueString(PsFileAccessorIni.PVIDLocation));
common.writeIniFileIdentify("Authentication", "VALID");// PsFileAccessorIni.GetInstance().GetValueString(PsFileAccessorIni.PVIDLocation));

property = new Properties();
property.load(fs);
System.out.println(property.getProperty("path.pvInfoIni"));
String pvId = property.getProperty("PV-ID");
String palmusId = property.getProperty("PALMUS-ID");
System.out.println(pvId);
System.out.println(palmusId);

And when I tried to run the program this is the output:

在此处输入图片说明

I'm not sure if it really goes to that path. If so why does the pvId and palmusId is null? I hope someone can help me. I'm just new with this kind of stuffs. Thank you in advance.

PS But when I use this code:

Properties p = new Properties();
p.load(new FileInputStream("C://Palmus-HACMS/PV/PVInfo.ini"));
String pvId = p.getProperty("PV-ID");
String palmusId = p.getProperty("PALMUS-ID");
System.out.println(pvId);
System.out.println(palmusId);

the value of PV-ID and PALMUS-ID is being output.

A Properties object can only load the key/value pairs that you have in the properties file nothing more, it is not capable of loading dynamically ini or properties files whose path could be defined as values because for it we have only String keys and values nothing more.

If you want to dynamically load also ini files, you will need to do it manually using for example ini4j .

One way to have everything into your Properties instance could be to proceed as next:

Properties properties = new Properties();
try (InputStream is = new FileInputStream("path.properties")) {
    properties.load(is);
}
// Then we load the key/value pairs in the ini file
try (InputStream is = new FileInputStream(properties.getProperty("path.pvInfoIni"))) {
    Wini wini = new Wini(is);
    for (Map.Entry<String, Profile.Section> entry : wini.entrySet()) {
        for (Map.Entry<String, String> subEntry : entry.getValue().entrySet()) {
            // Add the entry of the init file into the properties file
            // using ${section-name}.${key-name} as key and ${value} as value
            properties.put(
                String.format("%s.%s", entry.getKey(), subEntry.getKey()), 
                subEntry.getValue()
            );
        }
    }
}

If you don't want to add a prefix corresponding to the name of the section which prevents naming collision, you can simply replace String.format("%s.%s", entry.getKey(), subEntry.getKey()) with subEntry.getKey() in the code above but I don't personally believe that it is a good idea/practice.

Just change like this:

 p.load(new FileInputStream("C://Palmus-HACMS//PV//PVInfo.ini"));

To load file from resource folder :

Properties prop = new Properties();
            String propFileName = "PVInfo.ini";

            inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);

            if (inputStream != null) {
                prop.load(inputStream);
            } else {
                throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
            }

for more understating see this:

http://crunchify.com/java-properties-file-how-to-read-config-properties-values-in-java/

The file you are loading in the line property.load(fs) on your code is the original properties file, while I assume you intended to be loading the ini one. You need to get the path from the original properties file and create a new file using that one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM