在web中如何更改properties文件的内容,绝对经典!
2015-12-21 16:58
351 查看
public class PropUtil {
/** * 根据KEY,读取文件对应的值 *
@param filePath 文件路径,即文件所在包的路径,例如:java/util/config.properties *
@param key 键 *
@return key对应的值 */
public static String getProp(String filename, String key) {
try {
Properties props = new Properties();
String filepath = PropUtil.class.getClassLoader().getResource("/").getPath() + filename;
File file = new File(filepath);
InputStream in = new FileInputStream(file);
props.load(in);
in.close();
String value = props.getProperty(key);
return value;
} catch (Exception e)
{ e.printStackTrace(); return null; } }
/** * 修改或添加键值对 如果key存在,修改, 反之,添加。
* @param filePath 文件路径,即文件所在包的路径,例如:java/util/config.properties
* @param key 键 * @param value 键对应的值
*/
public static void setProp(String filename, String key, String value) {
try {
Properties prop = new Properties();
String filepath = PropUtil.class.getClassLoader().getResource("/").getPath() + filename;
File file = new File(filepath);
InputStream in = new FileInputStream(file);
prop.load(in); //一定要在修改值之前关闭fis
in.close();
System.out.println("filepath---------->" + filepath);
OutputStream fos = new FileOutputStream(file); prop.setProperty(key, value); //保存,并加入注释
prop.store(fos, "Update '" + key + "' value");
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
} } }
/** * 根据KEY,读取文件对应的值 *
@param filePath 文件路径,即文件所在包的路径,例如:java/util/config.properties *
@param key 键 *
@return key对应的值 */
public static String getProp(String filename, String key) {
try {
Properties props = new Properties();
String filepath = PropUtil.class.getClassLoader().getResource("/").getPath() + filename;
File file = new File(filepath);
InputStream in = new FileInputStream(file);
props.load(in);
in.close();
String value = props.getProperty(key);
return value;
} catch (Exception e)
{ e.printStackTrace(); return null; } }
/** * 修改或添加键值对 如果key存在,修改, 反之,添加。
* @param filePath 文件路径,即文件所在包的路径,例如:java/util/config.properties
* @param key 键 * @param value 键对应的值
*/
public static void setProp(String filename, String key, String value) {
try {
Properties prop = new Properties();
String filepath = PropUtil.class.getClassLoader().getResource("/").getPath() + filename;
File file = new File(filepath);
InputStream in = new FileInputStream(file);
prop.load(in); //一定要在修改值之前关闭fis
in.close();
System.out.println("filepath---------->" + filepath);
OutputStream fos = new FileOutputStream(file); prop.setProperty(key, value); //保存,并加入注释
prop.store(fos, "Update '" + key + "' value");
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
} } }
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- GUI - Web前端开发框架
- 介绍一款信息管理系统的开源框架---jeecg
- Extjs4.0 最新最全视频教程
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序