.properties文件中的文件夹路径

时间:2016-10-19 10:57:54

标签: java parsing properties path

我试图将所有路径(文件夹路径/网址)放在.properties文件中,这样如果我要更改路径,就会更容易。所以这是我的path.properties中的一个数据:

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

这是该文件的真实目录:

  

C:\心悸-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);

当我尝试运行程序时,这是输出:

enter image description here

我不确定它是否真的走向了这条道路。如果是这样,为什么pvId和palmusId为空?我希望有一个人可以帮助我。我对这种东西很新。提前谢谢。

P.S。 但是当我使用这段代码时:

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的值。

3 个答案:

答案 0 :(得分:1)

Properties对象只能加载属性文件中的键/值对,无法动态加载 ini或{{ 1}}文件的路径可以定义为值,因为我们只有properties个键和值。

如果您想动态加载String文件,则需要使用例如ini4j手动加载。

将所有内容放入ini实例的一种方法可能是继续下一步:

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()),但我不是个人相信这是一个好主意/实践。

答案 1 :(得分:1)

只需改变:

 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/

答案 2 :(得分:0)

您在代码行Private Sub Application_Quit() Dim objMsg As MailItem Dim EmailSubject As String Dim SendTo As String Set objMsg = Application.CreateItem(olMailItem) EmailSubject = "Outlook session Closing" SendTo = "xxxx@outlook.com" objMsg.To = SendTo objMsg.Subject = EmailSubject objMsg.Send End Sub 中加载的文件是原始属性文件,而我假设您打算加载property.load(fs)。您需要从原始属性文件中获取路径并使用该文件创建新文件。