jmpropertycatch.java

来自「梦界家园程序开发基底框架」· Java 代码 · 共 85 行

JAVA
85
字号
package jm.util;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
import java.util.ResourceBundle;

/**
 * JMProPertyCatch
 * @author fu_changyong(1shome)
 * @see jdk1.5.2
 * @version 1.5.2.2
 */
public class JMProPertyCatch {
    public static final String CLASS_NAME = "JMProPertyCatch";
    private String filename = null;
    private Properties props = new Properties();
    private final String PATH = "/";

    public JMProPertyCatch (String fileName) {
        filename = fileName;
    }

    public void loadProperties () throws Exception {
        try {
            props.load(getClass().getResourceAsStream(PATH + filename + ".properties"));
        } catch (Exception e) {
            throw e;
        }
    }

    public void loadXML () throws Exception {
        try {
            props.loadFromXML(new FileInputStream(PATH + filename + ".xml"));
        } catch (Exception e) {
            throw e;
        }
    }

    public void setFileName (String fileName) {
        filename = fileName;
    }

    public String getFileName () {
        return filename;
    }

    public String getProperty (String key) throws Exception {
        try {
            return props.getProperty(key);
        } catch (Exception e) {
            throw e;
        }
    }

    public void setProperty (String key, String val) throws Exception {
        try {
            props.setProperty(key, val);
        } catch (Exception e) {
            throw e;
        }
    }

    public void storeProperties (String Titles) throws Exception {
        try {
            props.store(new FileOutputStream(filename + ".properties"), Titles);
        } catch (Exception e) {
            throw e;
        }
    }

    public void storeXML (String Titles) throws Exception {
        try {
            props.storeToXML(new FileOutputStream(filename + ".xml"), Titles);
        } catch (Exception e) {
            throw e;
        }
    }

    public static String getProperties (String filename, String key) {
        ResourceBundle res = ResourceBundle.getBundle(filename);
        return res.getString(key);
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?