码迷,mamicode.com
首页 > 其他好文 > 详细

propertityUtil

时间:2016-05-27 10:45:58      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:

 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();
       }
     }
   }
 }

 

propertityUtil

标签:

原文地址:http://www.cnblogs.com/zhangxuesong/p/5533532.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!