关闭

How to use java Properties API -- read and write

785人阅读 评论(0) 收藏 举报

===========================Source Code============================================

package suncertify.utility;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
import java.util.logging.LogManager;

import suncertify.Constraints;
import suncertify.db.SystemException;

public class ConfiguraionUtil {
    public static final Properties readProperties()
            throws SystemException {
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream(
                    new File(Constraints.FILE_PROPERTIES)));
        } catch (FileNotFoundException e) {
            throw new SystemException(
                    "Can not find the suncertify.properties file");
        } catch (IOException e) {
            throw new SystemException("I/O error occurs when reading the file");
        }
        return properties;
    }

    public static final void writeProperties(final Hashtable table)
            throws SystemException {
        Properties properties = new Properties();
        FileOutputStream out = null;
        try {
            Set keys = table.keySet();
            Iterator iter = keys.iterator();
            String content = "";
            while (iter.hasNext()) {
                String key = (String) iter.next();
                Object value = table.get(key);
                content += key + "=" + value + "/n";
            }
            out = new FileOutputStream(new File(Constraints.FILE_PROPERTIES));
            out.write(content.getBytes());
            properties.store(out, null);
        } catch (FileNotFoundException e) {
            throw new SystemException(
                    "Can not find the suncertify.properties file");
        } catch (IOException e) {
            throw new SystemException("I/O error occurs when reading the file");
        } finally {
            try {
                out.close();   
            } catch(IOException e) {
                out = null;
            }
        }
    }

}

Reference:

http://www.dimi.uniud.it/labs/documentazione/java1.0-tutorial/java/cmdLineArgs/properties.html

http://javaalmanac.com/egs/java.util/Props.html

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:16825次
    • 积分:291
    • 等级:
    • 排名:千里之外
    • 原创:13篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条
    文章分类