Last active
August 29, 2015 14:02
-
-
Save daichan4649/e4b509dd4601b4b53066 to your computer and use it in GitHub Desktop.
Servletでのプロパティファイル読み書き
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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