java 解析properties文件 工具类 通用
来源:互联网 发布:人工智能后空翻 编辑:程序博客网 时间:2024/05/12 10:36
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class PropertiesUtil {
/**
*
* 方法: getProperties <br>
* 描述: 更具key获取value <br>
* 作者: 宋学军 sxjworld@126.com<br>
* 时间: 2013-12-6 下午3:13:52
* @param key
* @return
*/
public static String getProperties(String key) {
Properties prop = new Properties();
InputStream url = PropertiesUtil.class.getResourceAsStream("/conf/deploy.properties");
FileInputStream in = null;
try {
prop.load(url);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(in != null){
try {
in.close();
in= null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
return (String)prop.get(key);
}
public static String getProperties(String path,String key) {
Properties prop = new Properties();
InputStream url = null;
FileInputStream in = null;
try {
url = new FileInputStream(path);
} catch (FileNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
prop.load(url);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(in != null){
try {
in.close();
in= null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
return (String)prop.get(key);
}
public static void addProperties(String key,String value){
InputStream url = PropertiesUtil.class.getResourceAsStream("/conf/deploy.properties");
Properties prop = new Properties();
try {
prop.load(url);
} catch (IOException e) {
e.printStackTrace();
}
/*Map toSaveMap = new HashMap();
Set keys = prop.keySet();
for (Iterator itr = keys.iterator(); itr.hasNext();) {
String oldKey = (String) itr.next();
Object oldValue = prop.get(oldKey);
toSaveMap.put(oldKey, oldValue);
}
toSaveMap.put(key,value);
prop.putAll(toSaveMap);
System.out.println(toSaveMap.toString());*/
prop.put(key, value);
try
{
OutputStream outputStream = new FileOutputStream(new File(PropertiesUtil.class.getResource("/conf/deploy.properties").getFile()));
prop.store(outputStream, null);
outputStream.flush();
outputStream.close();
System.out.println("添加成功");
}
catch (IOException e)
{
e.printStackTrace();
}
}
/**
*
* 方法: addProperties <br>
* 描述: 设置key,value <br>
* 作者: 宋学军 sxjworld@126.com<br>
* 时间: 2013-12-6 下午3:15:03
* @param key
* @param value
* @throws Exception
*/
public static void addProperties(String path,String key,String value){
InputStream url = null;
try {
url = new FileInputStream(path);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Properties prop = new Properties();
try {
prop.load(url);
} catch (IOException e) {
e.printStackTrace();
}
Map toSaveMap = new HashMap();
Set keys = prop.keySet();
for (Iterator itr = keys.iterator(); itr.hasNext();) {
String oldKey = (String) itr.next();
Object oldValue = prop.get(oldKey);
toSaveMap.put(oldKey, oldValue);
}
toSaveMap.put(key,value);
prop.putAll(toSaveMap);
System.out.println(toSaveMap.toString());
try
{
OutputStream outputStream = new FileOutputStream(new File(path));
prop.store(outputStream, null);
outputStream.flush();
outputStream.close();
System.out.println("添加成功");
}
catch (IOException e)
{
e.printStackTrace();
}
}
/**
*
* 方法: updateProperties <br>
* 描述: 修改 <br>
* 作者: 宋学军 sxjworld@126.com<br>
* 时间: 2013-12-6 下午3:28:29
* @param key
* @param value
*/
public static void updateProperties(String key,String value) {
InputStream url = PropertiesUtil.class.getResourceAsStream("/conf/deploy.properties");
Properties prop = new Properties();
try {
prop.load(url);
} catch (IOException e) {
e.printStackTrace();
}
/*Map toSaveMap = new HashMap();
Set keys = prop.keySet();
for (Iterator itr = keys.iterator(); itr.hasNext();) {
String oldKey = (String) itr.next();
Object oldValue = prop.get(oldKey);
toSaveMap.put(oldKey, oldValue);
}
toSaveMap.put(key,value);
prop.putAll(toSaveMap);
System.out.println(toSaveMap.toString());*/
prop.put(key, value);
try
{
OutputStream outputStream = new FileOutputStream(new File(PropertiesUtil.class.getResource("/conf/deploy.properties").getFile()));
prop.store(outputStream, null);
outputStream.flush();
outputStream.close();
System.out.println("修改成功");
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static void updateProperties(String path,String key,String value) {
InputStream url = null;
try {
url = new FileInputStream(path);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Properties prop = new Properties();
try {
prop.load(url);
} catch (IOException e) {
e.printStackTrace();
}
/*Map toSaveMap = new HashMap();
Set keys = prop.keySet();
for (Iterator itr = keys.iterator(); itr.hasNext();) {
String oldKey = (String) itr.next();
Object oldValue = prop.get(oldKey);
toSaveMap.put(oldKey, oldValue);
}
toSaveMap.put(key,value);
prop.putAll(toSaveMap);
System.out.println(toSaveMap.toString());*/
prop.put(key, value);
try
{
OutputStream outputStream = new FileOutputStream(new File(path));
prop.store(outputStream, null);
outputStream.flush();
outputStream.close();
System.out.println("修改成功");
}
catch (IOException e)
{
e.printStackTrace();
}
}
/**
*
* 方法: delProperties <br>
* 描述: 删除 <br>
* 作者: 宋学军 sxjworld@126.com<br>
* 时间: 2013-12-6 下午9:53:28
* @param key
*/
public void delProperties(String key)
{
//将文件中key,value先拿出来
InputStream url = PropertiesUtil.class.getResourceAsStream("/conf/deploy.properties");
Properties prop = new Properties();
try {
prop.load(url);
} catch (IOException e) {
e.printStackTrace();
}
/*Map toSaveMap = new HashMap();
Set keys = prop.keySet();
for (Iterator itr = keys.iterator(); itr.hasNext();)
{
String oldKey = (String) itr.next();
Object oldValue = prop.get(oldKey);
toSaveMap.put(oldKey, oldValue);
}
prop.clear();
toSaveMap.remove(key);
prop.putAll(toSaveMap);*/
prop.remove(key);
//将所有键值对再写入文件
try
{
FileOutputStream outputStream = new FileOutputStream(PropertiesUtil.class.getResource("/conf/deploy.properties").getFile());
prop.store(outputStream, null);
outputStream.flush();
outputStream.close();
System.out.println("删除成功");
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void delProperties(String path,String key)
{
//将文件中key,value先拿出来
InputStream url = null;
try {
url = new FileInputStream(path);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Properties prop = new Properties();
try {
prop.load(url);
} catch (IOException e) {
e.printStackTrace();
}
/*Map toSaveMap = new HashMap();
Set keys = prop.keySet();
for (Iterator itr = keys.iterator(); itr.hasNext();)
{
String oldKey = (String) itr.next();
Object oldValue = prop.get(oldKey);
toSaveMap.put(oldKey, oldValue);
}
prop.clear();
toSaveMap.remove(key);
prop.putAll(toSaveMap);
System.out.println(toSaveMap.toString());*/
prop.remove(key);
//将所有键值对再写入文件
try
{
FileOutputStream outputStream = new FileOutputStream(path);
prop.store(outputStream, null);
outputStream.flush();
outputStream.close();
System.out.println("删除成功");
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class PropertiesUtil {
/**
*
* 方法: getProperties <br>
* 描述: 更具key获取value <br>
* 作者: 宋学军 sxjworld@126.com<br>
* 时间: 2013-12-6 下午3:13:52
* @param key
* @return
*/
public static String getProperties(String key) {
Properties prop = new Properties();
InputStream url = PropertiesUtil.class.getResourceAsStream("/conf/deploy.properties");
FileInputStream in = null;
try {
prop.load(url);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(in != null){
try {
in.close();
in= null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
return (String)prop.get(key);
}
public static String getProperties(String path,String key) {
Properties prop = new Properties();
InputStream url = null;
FileInputStream in = null;
try {
url = new FileInputStream(path);
} catch (FileNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
prop.load(url);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(in != null){
try {
in.close();
in= null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
return (String)prop.get(key);
}
public static void addProperties(String key,String value){
InputStream url = PropertiesUtil.class.getResourceAsStream("/conf/deploy.properties");
Properties prop = new Properties();
try {
prop.load(url);
} catch (IOException e) {
e.printStackTrace();
}
/*Map toSaveMap = new HashMap();
Set keys = prop.keySet();
for (Iterator itr = keys.iterator(); itr.hasNext();) {
String oldKey = (String) itr.next();
Object oldValue = prop.get(oldKey);
toSaveMap.put(oldKey, oldValue);
}
toSaveMap.put(key,value);
prop.putAll(toSaveMap);
System.out.println(toSaveMap.toString());*/
prop.put(key, value);
try
{
OutputStream outputStream = new FileOutputStream(new File(PropertiesUtil.class.getResource("/conf/deploy.properties").getFile()));
prop.store(outputStream, null);
outputStream.flush();
outputStream.close();
System.out.println("添加成功");
}
catch (IOException e)
{
e.printStackTrace();
}
}
/**
*
* 方法: addProperties <br>
* 描述: 设置key,value <br>
* 作者: 宋学军 sxjworld@126.com<br>
* 时间: 2013-12-6 下午3:15:03
* @param key
* @param value
* @throws Exception
*/
public static void addProperties(String path,String key,String value){
InputStream url = null;
try {
url = new FileInputStream(path);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Properties prop = new Properties();
try {
prop.load(url);
} catch (IOException e) {
e.printStackTrace();
}
Map toSaveMap = new HashMap();
Set keys = prop.keySet();
for (Iterator itr = keys.iterator(); itr.hasNext();) {
String oldKey = (String) itr.next();
Object oldValue = prop.get(oldKey);
toSaveMap.put(oldKey, oldValue);
}
toSaveMap.put(key,value);
prop.putAll(toSaveMap);
System.out.println(toSaveMap.toString());
try
{
OutputStream outputStream = new FileOutputStream(new File(path));
prop.store(outputStream, null);
outputStream.flush();
outputStream.close();
System.out.println("添加成功");
}
catch (IOException e)
{
e.printStackTrace();
}
}
/**
*
* 方法: updateProperties <br>
* 描述: 修改 <br>
* 作者: 宋学军 sxjworld@126.com<br>
* 时间: 2013-12-6 下午3:28:29
* @param key
* @param value
*/
public static void updateProperties(String key,String value) {
InputStream url = PropertiesUtil.class.getResourceAsStream("/conf/deploy.properties");
Properties prop = new Properties();
try {
prop.load(url);
} catch (IOException e) {
e.printStackTrace();
}
/*Map toSaveMap = new HashMap();
Set keys = prop.keySet();
for (Iterator itr = keys.iterator(); itr.hasNext();) {
String oldKey = (String) itr.next();
Object oldValue = prop.get(oldKey);
toSaveMap.put(oldKey, oldValue);
}
toSaveMap.put(key,value);
prop.putAll(toSaveMap);
System.out.println(toSaveMap.toString());*/
prop.put(key, value);
try
{
OutputStream outputStream = new FileOutputStream(new File(PropertiesUtil.class.getResource("/conf/deploy.properties").getFile()));
prop.store(outputStream, null);
outputStream.flush();
outputStream.close();
System.out.println("修改成功");
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static void updateProperties(String path,String key,String value) {
InputStream url = null;
try {
url = new FileInputStream(path);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Properties prop = new Properties();
try {
prop.load(url);
} catch (IOException e) {
e.printStackTrace();
}
/*Map toSaveMap = new HashMap();
Set keys = prop.keySet();
for (Iterator itr = keys.iterator(); itr.hasNext();) {
String oldKey = (String) itr.next();
Object oldValue = prop.get(oldKey);
toSaveMap.put(oldKey, oldValue);
}
toSaveMap.put(key,value);
prop.putAll(toSaveMap);
System.out.println(toSaveMap.toString());*/
prop.put(key, value);
try
{
OutputStream outputStream = new FileOutputStream(new File(path));
prop.store(outputStream, null);
outputStream.flush();
outputStream.close();
System.out.println("修改成功");
}
catch (IOException e)
{
e.printStackTrace();
}
}
/**
*
* 方法: delProperties <br>
* 描述: 删除 <br>
* 作者: 宋学军 sxjworld@126.com<br>
* 时间: 2013-12-6 下午9:53:28
* @param key
*/
public void delProperties(String key)
{
//将文件中key,value先拿出来
InputStream url = PropertiesUtil.class.getResourceAsStream("/conf/deploy.properties");
Properties prop = new Properties();
try {
prop.load(url);
} catch (IOException e) {
e.printStackTrace();
}
/*Map toSaveMap = new HashMap();
Set keys = prop.keySet();
for (Iterator itr = keys.iterator(); itr.hasNext();)
{
String oldKey = (String) itr.next();
Object oldValue = prop.get(oldKey);
toSaveMap.put(oldKey, oldValue);
}
prop.clear();
toSaveMap.remove(key);
prop.putAll(toSaveMap);*/
prop.remove(key);
//将所有键值对再写入文件
try
{
FileOutputStream outputStream = new FileOutputStream(PropertiesUtil.class.getResource("/conf/deploy.properties").getFile());
prop.store(outputStream, null);
outputStream.flush();
outputStream.close();
System.out.println("删除成功");
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void delProperties(String path,String key)
{
//将文件中key,value先拿出来
InputStream url = null;
try {
url = new FileInputStream(path);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Properties prop = new Properties();
try {
prop.load(url);
} catch (IOException e) {
e.printStackTrace();
}
/*Map toSaveMap = new HashMap();
Set keys = prop.keySet();
for (Iterator itr = keys.iterator(); itr.hasNext();)
{
String oldKey = (String) itr.next();
Object oldValue = prop.get(oldKey);
toSaveMap.put(oldKey, oldValue);
}
prop.clear();
toSaveMap.remove(key);
prop.putAll(toSaveMap);
System.out.println(toSaveMap.toString());*/
prop.remove(key);
//将所有键值对再写入文件
try
{
FileOutputStream outputStream = new FileOutputStream(path);
prop.store(outputStream, null);
outputStream.flush();
outputStream.close();
System.out.println("删除成功");
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
0 0
- java 解析properties文件 工具类 通用
- java解析properties文件
- java解析properties文件
- java 解析properties 文件
- Java Properties文件解析
- Java解析Properties文件
- java解析properties文件
- java解析Properties文件
- properties 资源文件解析工具
- 【java工具类】properties文件操作
- java读取Properties文件的工具类
- Java工具类--读取Properties文件
- Java读取Properties文件工具类
- Java属性Properties文件读取工具类
- Java工具类--读取Properties文件
- Java读取Properties文件工具类
- java读取properties文件工具类
- Java中Properties文件读取工具类
- js验证电话号码
- Struts2中ModelDriven的使用
- VS 提示0xcdcdcdcd的错误
- Yii cookie
- Hbase访问方式之Mapreduce
- java 解析properties文件 工具类 通用
- (10)Java泛型-Map集合-集合框架工具类-可变参数-静态导入
- 将反射用于工厂模式
- Oracle Join Methods
- IOS-将长文字转化成图片方法
- [Leetcode] Multiply Strings (Java)
- ROW_NUMBER() OVER函数的基本用法
- Oracle聚合函数RANK和dense_rank的使用
- DLL中调用约定和名称修饰(二)