Skip to content

Instantly share code, notes, and snippets.

@daichan4649
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daichan4649/e4b509dd4601b4b53066 to your computer and use it in GitHub Desktop.
Save daichan4649/e4b509dd4601b4b53066 to your computer and use it in GitHub Desktop.
Servletでのプロパティファイル読み書き
public class PropertyUtil {
private static final String PROPERTY_FILE_TEST = "test.properties";
public static void setTestPropertyValue(ServletContext context, String key, String value) {
setPropertyValue(context, PROPERTY_FILE_TEST, key, value);
}
public static String getTestPropertyValue(ServletContext context, String key) {
return getPropertyValue(context, PROPERTY_FILE_TEST, key);
}
public static void setPropertyValue(ServletContext context, String propertyFileName, String key, String value) {
String realPath = getPropertyFilePath(context, propertyFileName);
Properties prop = new Properties();
try {
prop.load(new FileInputStream(realPath));
prop.setProperty(key, value);
prop.store(new FileOutputStream(realPath), null);
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getPropertyValue(ServletContext context, String propertyFileName, String key) {
String realPath = getPropertyFilePath(context, propertyFileName);
Properties prop = new Properties();
try {
prop.load(new FileInputStream(realPath));
return prop.getProperty(key);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private static String getPropertyFilePath(ServletContext context, String propertyFileName) {
String path = String.format("WEB-INF/classes/%s", propertyFileName);
return context.getRealPath(path);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment