标签:
package com.adesk.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.Date;
import java.util.Properties;
public class PropertiesUtil
{
public static Properties loadRealPathProps(String propfile)
throws IOException
{
InputStream in = new FileInputStream(propfile);
Reader reader = new InputStreamReader(in, Conf.charSet);
Properties p = new Properties();
p.load(reader);
try
{
reader.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return p;
}
public static Properties loadClassPathProps(String propfile)
throws IOException
{
InputStream in = PropertiesUtil.class.getResourceAsStream(propfile);
Reader reader = new InputStreamReader(in, Conf.charSet);
Properties p = new Properties();
p.load(reader);
try
{
reader.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return p;
}
public static void addOrUpdateRecord(String key, String value, String path)
{
File file = new File(path);
Writer out = null;
FileOutputStream fos = null;
Properties p = null;
try {
try {
if (!file.exists()) {
FileUtil.createFile(file);
}
p = loadRealPathProps(path);
} catch (Exception e) {
p = new Properties();
}
fos = new FileOutputStream(path);
out = new OutputStreamWriter(fos, Conf.charSet);
p.setProperty(key, value==null?"":value);
p.store(out, "update timestamp:" + new Date().toLocaleString());
} catch (IOException e) {
e.printStackTrace();
try
{
if (fos != null)
fos.close();
if (out != null)
out.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
finally
{
try
{
if (fos != null)
fos.close();
if (out != null)
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void storeProp(Properties prop, String realPath)
{
Writer out = null;
FileOutputStream fos = null;
try {
FileUtil.createFile(new File(realPath));
fos = new FileOutputStream(realPath);
out = new OutputStreamWriter(fos, Conf.charSet);
prop.store(out, "update timestamp:" + new Date().toLocaleString());
} catch (IOException e) {
e.printStackTrace();
try
{
if (fos != null)
fos.close();
if (out != null)
out.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
finally
{
try
{
if (fos != null)
fos.close();
if (out != null)
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
标签:
原文地址:http://www.cnblogs.com/zhangxuesong/p/5533532.html