[英]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”。請提出建議。
從文檔中,您可以看到props.getProperty(String propertyName)
將獲取propertyName
鍵的值。 您所做的是使用props.getProperty(String propertyName, String defaultValue)
,除非找不到applications
,否則不會獲得environment
。
我會假設你想要他們兩個。 為此,您應該執行以下操作:
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];
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];
編輯
根據您的評論,如果您現在想從您擁有的內容中獲取abc.csv
,我建議您使用StringBuilder
( docs )
基本上,您想將拆分后的字符串和 append 帶到.csv
字符串中。 這是按如下方式完成的:
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"
對於最后一條評論,只需執行以下操作:
StringBuilder file2 = new StringBuilder();
file2.append(appNameSplitted[1])
.append("-")
.append(envNameSplitted[1])
.append(".csv");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.