[英]Folder Path in .properties file
我试图将所有路径(文件夹路径/ URL)放在.properties文件中,这样,如果我要更改路径,这将变得更加容易。 因此,这是我的path.properties中的数据之一:
path.pvInfoIni=C:\\Palmus-HACMS\\PV\\PvInfo.ini
这是文件的真实目录:
C:\\ Palmus-HACMS \\ PV \\ PVInfo.ini
该路径将导致写入数据的.ini文件。
这是我称之为路径的代码:
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);
当我尝试运行程序时,这是输出:
我不确定它是否真的走了那条路。 如果是这样,为什么pvId和palmusId为null? 我希望有一个人可以帮助我。 我刚刚接触这种东西。 先感谢您。
PS,但是当我使用此代码时:
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);
PV-ID和PALMUS-ID的值正在输出。
Properties
对象只能再加载属性文件中具有的键/值对, 它不能动态加载 ini
或可以将路径定义为值的properties
文件,因为它只有String
键和值更多。
如果要同时动态加载ini
文件,则需要使用ini4j等手动进行。
将所有内容都放入“ Properties
实例的一种方法是继续进行以下操作:
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()
);
}
}
}
如果您不想添加与防止命名冲突的部分名称相对应的前缀,则只需替换String.format("%s.%s", entry.getKey(), subEntry.getKey())
在上面的代码中使用subEntry.getKey()
,但是我个人并不认为这是个好主意/做法。
只是这样改变:
p.load(new FileInputStream("C://Palmus-HACMS//PV//PVInfo.ini"));
要从资源文件夹加载文件:
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");
}
欲了解更多信息,请参见:
http://crunchify.com/java-properties-file-how-to-read-config-properties-values-in-java/
您在代码行property.load(fs)
中加载的文件是原始属性文件,而我假设您打算加载ini
。 您需要从原始属性文件中获取路径,并使用该文件创建一个新文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.