如何获取文件中键的第一个值?

huangapple go评论61阅读模式
英文:

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");

huangapple
  • 本文由 发表于 2020年7月28日 15:56:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63129460.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定