jspの.propertiesファイルにキーと値のペアのような値でパラメータの値を保存する方法
例えば。
URL:
http://www.xyz.com/login.jsp ? fname=harry&lname=potter&occupation=actor
.property
ファイル
は
次のようになっている必要があります
出来ますか?
前もって感謝します
これはどうですか:
final String urlString = "http://www.xyz.com/login.jsp?fname=harry&lname=potter&occupation=actor";
final URL url;
try {
url = new URL(urlString);
} catch (MalformedURLException ex) {
throw new RuntimeException(ex);
}
final Properties properties = new Properties();
for (final String param : url.getQuery().split("\\&")) {
final String[] splitParam = param.split("=");
properties.setProperty(splitParam[0], splitParam[1]);
}
for (final String key : properties.stringPropertyNames()) {
System.out.println("Key " + key + " has value " + properties.getProperty(key) + ".");
}
final FileOutputStream fileOutputStream;
try {
fileOutputStream = new FileOutputStream(new File("My File"));
} catch (FileNotFoundException ex) {
throw new RuntimeException(ex);
}
try {
properties.store(fileOutputStream, "Properties from URL '" +urlString + "'.");
} catch(IOException ex) {
throw new RuntimeException(ex);
} finally {
try {
fileOutputStream.close();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
これにより、URL が解析され、params がProperties
オブジェクトに入れられ、ファイルに書き込まれます。
URL 文字列に重複するキーがある場合、それらは上書きされるため、この方法は機能しないことに注意してください。その場合は、Apache HttpComponentsを確認することをお勧めします。
チェックjava.util.Properties.store(OutputStream, String)
してjava.util.Properties.store(Writer, String)
(Java 1.6)