英文:
How to get first value of the key from the file?
问题
我正在尝试获取键的第一个值,但它打印了两个值。有人可以提供建议吗?
Properties props = new Properties();
props.load(new FileReader(configFilePath));
String appName = props.getProperty("app","env");
System.out.println("AppName:"+appName);
我只需要打印"abc"作为应用程序,环境作为"ab"。请提供建议。
英文:
I am trying to get the first value of the key but it is printing two values. Can anyone please suggest me.
Properties props = new Properties();
props.load(new FileReader(configFilePath));
String appName = props.getProperty("app","env");
System.out.println("AppName:"+appName);
I need to print only "abc" as applications and environment as "ab".Please suggest.
答案1
得分: 1
From the docs, you can see that props.getProperty(String propertyName)
will get your value for the propertyName
key. What you did was you used props.getProperty(String propertyName, String defaultValue)
, which won't get environment
unless applications
is not found.
I will assume you want both of them. For this, you should do the following:
String appName = props.getProperty("applications");
String[] appNameSplitted = appName.split(",");
// to get 'abc', do
String firstAppName = appNameSplitted[0];
// to get 'tds' do
String secondAppName = appNameSplitted[1];
The same goes for environment
:
String envName = props.getProperty("environment");
String[] envNameSplitted = envName.split(",");
// to get 'ab', do
String firstEnvName = envNameSplitted[0];
// to get 'bc' do
String secondEnvName = envNameSplitted[1];
EDIT
Based on your comment, if you want now to get abc.csv
from what you have, I suggest you use StringBuilder
(docs)
Basically, you want to take your splitted string and append to it the .csv
string. This is done as follows:
StringBuilder file1 = new StringBuilder();
file1.append(appNameSplitted[0])
.append(".csv");
// the same goes for all the other options
// to get the string value of file1, use
file1.toString(); // this is now "abc.csv";
For the last comment, simply do:
StringBuilder file2 = new StringBuilder();
file2.append(appNameSplitted[1])
.append("-")
.append(envNameSplitted[1])
.append(".csv");
英文:
From the docs, you can see that props.getProperty(String propertyName)
will get your value for the propertyName
key. What you did was you used props.getProperty(String propertyName, String defaultValue)
, which won't get environment
unless applications
is not found.
I will assume you want both of them. For this, you should do the following:
String appName = props.getProperty("applications");
String[] appNameSplitted = appName.split(","); // split after comma
// to get 'abc', do
String firstAppName = appNameSplitted[0];
// to get 'tds' do
String secondAppName = appNameSplitted[1];
The same goes for environment
:
String envName = props.getProperty("environment");
String[] envNameSplitted = envName.split(","); // split after comma
// to get 'ab', do
String firstEnvName = envNameSplitted[0];
// to get 'bc' do
String secondEnvName = envNameSplitted[1];
EDIT
Based on your comment, if you want now to get abc.csv
from what you have, I suggest you use StringBuilder
(docs)
Basically, you want to take your splitted string and append to it the .csv
string. This is done as follows:
StringBuilder file1 = new StringBuilder();
file1.append(appNameSplitted[0])
.append(".csv");
// the same goes for all the other options
// to get the string value of file1, use
file1.toString(); // this is now "abc.csv"
For the last comment, simply do:
StringBuilder file2 = new StringBuilder();
file2.append(appNameSplitted[1])
.append("-")
.append(envNameSplitted[1])
.append(".csv");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论