在 java 的“ ”中使用 属性 字段值?
Using a property field value within ' ' in java?
我的项目中有一个 属性 文件,比如说 config.properties,有一个 属性 字段 project.searchkey
。我可以将此字段的值设置为 project.searchkey = 'one','two'
吗?
是否会同时考虑 带有 '' 符号 的两个值?
使用 java.util.Properties
(see API)
public class Main {
public static void main(String[] args) {
Properties prop = new Properties();
try {
prop.load(Main.class.getClassLoader().getResourceAsStream("config.properties"));
String propertyValue = prop.getProperty("project.searchkey");
System.out.println(propertyValue);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
它打印 'one','two'
,因此它读取 =
之后的所有内容作为单个字符串
project.searchkey='one','two'
returns 'one','two'
project.searchkey=one,two
returns one,two
project.searchkey=one, 'two'
returns one, 'two'
等...
project.searchkey=one, two, \
three, four, \
five
最好不要使用逗号作为键,因此不需要单引号。
检索键 "project.searchkey"
:
的字符串值后
String value = bundle.getProperty("project.searchkey");
// value is "one, two, three, four, five"
String[] searchKeys = value.split(",\s*"); // Split by comma and any whitespace.
当然可以删除单引号以获得价值。
我的项目中有一个 属性 文件,比如说 config.properties,有一个 属性 字段 project.searchkey
。我可以将此字段的值设置为 project.searchkey = 'one','two'
吗?
是否会同时考虑 带有 '' 符号 的两个值?
使用 java.util.Properties
(see API)
public class Main {
public static void main(String[] args) {
Properties prop = new Properties();
try {
prop.load(Main.class.getClassLoader().getResourceAsStream("config.properties"));
String propertyValue = prop.getProperty("project.searchkey");
System.out.println(propertyValue);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
它打印 'one','two'
,因此它读取 =
之后的所有内容作为单个字符串
project.searchkey='one','two'
returns 'one','two'
project.searchkey=one,two
returns one,two
project.searchkey=one, 'two'
returns one, 'two'
等...
project.searchkey=one, two, \
three, four, \
five
最好不要使用逗号作为键,因此不需要单引号。
检索键 "project.searchkey"
:
String value = bundle.getProperty("project.searchkey");
// value is "one, two, three, four, five"
String[] searchKeys = value.split(",\s*"); // Split by comma and any whitespace.
当然可以删除单引号以获得价值。