如何读取java属性文件并获取其值?
在Ruby中,我有一个sample.yml文件,如下所示如何读取java属性文件并获取其值?,java,properties,Java,Properties,在Ruby中,我有一个sample.yml文件,如下所示 1_Group1: path:asdf filename:zxcv 2_Group2: path:qwer filename:poiu etc etc............ 现在我需要一个Example.propertiesJava文件,它应该包含如上所述的数据 使用Java,我想阅读Example.properties文件 然后需要根据组的数量迭代内容,并获得相应的“path”和“filename”值 例如:如
1_Group1:
path:asdf
filename:zxcv
2_Group2:
path:qwer
filename:poiu
etc etc............
现在我需要一个Example.propertiesJava文件,它应该包含如上所述的数据
使用Java,我想阅读Example.properties文件
然后需要根据组的数量迭代内容,并获得相应的“path”和“filename”值
例如:如果有5组。。第1组、第2组……第5组
然后我需要迭代
for(int i=0; i< noofgroups(5);i++){
........................
String slave = aaa.getString("path");
aaa.getString("filename");
}
它正在工作(我可以读取并获取值)
但我需要有“路径”和“文件名”这两个键。所以我需要对它们进行分组。如果您想使用
yaml
格式,您可以找到
您可以这样使用它:
YamlReader reader = new YamlReader(new FileReader("sample.yml"));
Map map = (Map)reader.read();
System.out.println(map.get("1_Group1"));
key_1=path_with_file_1
key_2=path_with_file_2
如果您想使用
yaml
格式,您可以找到
您可以这样使用它:
YamlReader reader = new YamlReader(new FileReader("sample.yml"));
Map map = (Map)reader.read();
System.out.println(map.get("1_Group1"));
key_1=path_with_file_1
key_2=path_with_file_2
有很多方法可以做到这一点;最自然的可能是使用自然的层次结构格式,如YAML、JSON或XML 另一种选择是使用其中一种技术,例如 如果您想使用“纯”属性文件,我建议您只需读取属性,按句点拆分属性名称,并存储到映射或类,这样您就可以:
1_Group1.path=asdf
1_Group1.filename:zxcv
2_Group2.path=qwer
2_Group2.filename=poiu
有很多方法可以做到这一点;最自然的可能是使用自然的层次结构格式,如YAML、JSON或XML 另一种选择是使用其中一种技术,例如 如果您想使用“纯”属性文件,我建议您只需读取属性,按句点拆分属性名称,并存储到映射或类,这样您就可以:
1_Group1.path=asdf
1_Group1.filename:zxcv
2_Group2.path=qwer
2_Group2.filename=poiu
您可以通过以下方式使用
java.utils.Properties
:
public static void loadPropertiesAndParse() {
Properties props = new Properties();
String propsFilename = "path_to_props_file";
FileInputStream in = new FileInputStream(propsFilename);
props.load(in);
Enumeration en = props.keys();
while (en.hasMoreElements()) {
String tmpValue = (String) en.nextElement();
String path = tmpValue.substring(0, tmpValue.lastIndexOf(File.separator)); // Get the path
String filename = tmpValue.substring(tmpValue.lastIndexOf(File.separator) + 1, tmpValue.length()); // Get the filename
}
}
您的属性
文件将如下所示:
YamlReader reader = new YamlReader(new FileReader("sample.yml"));
Map map = (Map)reader.read();
System.out.println(map.get("1_Group1"));
key_1=path_with_file_1
key_2=path_with_file_2
您可以通过以下方式使用
java.utils.Properties
:
public static void loadPropertiesAndParse() {
Properties props = new Properties();
String propsFilename = "path_to_props_file";
FileInputStream in = new FileInputStream(propsFilename);
props.load(in);
Enumeration en = props.keys();
while (en.hasMoreElements()) {
String tmpValue = (String) en.nextElement();
String path = tmpValue.substring(0, tmpValue.lastIndexOf(File.separator)); // Get the path
String filename = tmpValue.substring(tmpValue.lastIndexOf(File.separator) + 1, tmpValue.length()); // Get the filename
}
}
您的属性
文件将如下所示:
YamlReader reader = new YamlReader(new FileReader("sample.yml"));
Map map = (Map)reader.read();
System.out.println(map.get("1_Group1"));
key_1=path_with_file_1
key_2=path_with_file_2
我现在可以读取该文件并检索我需要的内容。但我需要有3或4个键作为“路径”和“文件名”。因此,我提出了一个问题,我可以阅读该文件并检索我现在需要的内容。但我需要有3或4个键作为“路径”和“文件名”。所以我提出了这个问题谢谢。但是我们不能使用sample.properties?@Prince是的,你不能,因为
sample.properties
使用java.util.properties
是的,我已经在使用java.util.properties了。但我不知道怎么用它来处理我的案子谢谢。但是我们不能使用sample.properties?@Prince是的,你不能,因为sample.properties
使用java.util.properties
是的,我已经在使用java.util.properties了。但我不知道如何在我的案件中使用它