「Java」Properties.stringPropertyNamesメソッドでプロパティのキーと値を取得する
1.関数説明
public Set<String> stringPropertyNames()
キーとそれに対応する値が文字列であるようなこのプロパティーリスト内のキーのセットを返します。キーまたは値が String 型でないプロパティーは省略されます。
2.プロパティ名 study.properties
language =java
purpose = study skill
language =java
purpose = study skill
language =java purpose = study skill
3.使用例
package com.arkgame.study.st2;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertiesDemo {
public static void main(String[] args) {
String path = "/com/arkgame/study.properties";
InputStream in = PropertiesDemo.class.getResourceAsStream(path);
if (in == null) {
throw new NullPointerException();
}
try {
Properties props = new Properties();
props.load(in);
// propKeyVal関数を呼び出す
propKeyVal(props);
} catch (IOException e) {
e.printStackTrace();
}
}
// プロパティ名のキーとvalueを取得
public static void propKeyVal(Properties props) {
if (props == null) {
throw new NullPointerException();
}
//キーとそれに対応する値が文字列であるようなこのプロパティーリスト内のキーのセット
for (String key : props.stringPropertyNames()) {
String value = props.getProperty(key);
System.out.println("key: " + key + " value:" + value);
}
}
}
package com.arkgame.study.st2;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertiesDemo {
public static void main(String[] args) {
String path = "/com/arkgame/study.properties";
InputStream in = PropertiesDemo.class.getResourceAsStream(path);
if (in == null) {
throw new NullPointerException();
}
try {
Properties props = new Properties();
props.load(in);
// propKeyVal関数を呼び出す
propKeyVal(props);
} catch (IOException e) {
e.printStackTrace();
}
}
// プロパティ名のキーとvalueを取得
public static void propKeyVal(Properties props) {
if (props == null) {
throw new NullPointerException();
}
//キーとそれに対応する値が文字列であるようなこのプロパティーリスト内のキーのセット
for (String key : props.stringPropertyNames()) {
String value = props.getProperty(key);
System.out.println("key: " + key + " value:" + value);
}
}
}
package com.arkgame.study.st2; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class PropertiesDemo { public static void main(String[] args) { String path = "/com/arkgame/study.properties"; InputStream in = PropertiesDemo.class.getResourceAsStream(path); if (in == null) { throw new NullPointerException(); } try { Properties props = new Properties(); props.load(in); // propKeyVal関数を呼び出す propKeyVal(props); } catch (IOException e) { e.printStackTrace(); } } // プロパティ名のキーとvalueを取得 public static void propKeyVal(Properties props) { if (props == null) { throw new NullPointerException(); } //キーとそれに対応する値が文字列であるようなこのプロパティーリスト内のキーのセット for (String key : props.stringPropertyNames()) { String value = props.getProperty(key); System.out.println("key: " + key + " value:" + value); } } }
4.実行結果
key: language value : java
key: purpose value: study skill
key: language value : java
key: purpose value: study skill
key: language value : java key: purpose value: study skill