Properties 操作

public class PropertyFilesUtil {

    public static void main(String[] args) throws IOException {

        String propertyFileName = "DB.properties";
        String xmlFileName = "DB.xml";
        writePropertyFile(propertyFileName, xmlFileName);
        readPropertyFile(propertyFileName, xmlFileName);
        readAllKeys(propertyFileName, xmlFileName);
        readPropertyFileFromClasspath(propertyFileName);
        readPropertyFileFromClasspathWithClassLoader(propertyFileName);
        readPropertyFileFromClasspathWithClass(propertyFileName);
        readPropertyFileFromClasspathWithStream(propertyFileName);
        readPropertyFileFromClasspathWithBundle(propertyFileName);
    }

    /**
     * read property file from classpath(src目录)
     * 
     * @param propertyFileName
     * @throws IOException
     */
    private static void readPropertyFileFromClasspath(String propertyFileName) throws IOException {

        Properties prop = new Properties();
        prop.load(PropertyFilesUtil.class.getClassLoader().getResourceAsStream(propertyFileName));
        System.out.println(propertyFileName + " loaded from Classpath::db.host = " + prop.getProperty("db.host"));
        System.out.println(propertyFileName + " loaded from Classpath::db.user = " + prop.getProperty("db.user"));
        System.out.println(propertyFileName + " loaded from Classpath::db.pwd = " + prop.getProperty("db.pwd"));
        System.out.println(propertyFileName + " loaded from Classpath::XYZ = " + prop.getProperty("XYZ"));

    }

    /**
     * read property file from classpath(src目录)
     * 
     * @param propertyFileName
     * @throws IOException
     */
    private static void readPropertyFileFromClasspathWithClassLoader(String propertyFileName) throws IOException {

        Properties prop = new Properties();
        prop.load(ClassLoader.getSystemResourceAsStream(propertyFileName));
        System.out.println(propertyFileName + " loaded from Classpath::db.host = " + prop.getProperty("db.host"));
        System.out.println(propertyFileName + " loaded from Classpath::db.user = " + prop.getProperty("db.user"));
        System.out.println(propertyFileName + " loaded from Classpath::db.pwd = " + prop.getProperty("db.pwd"));
        System.out.println(propertyFileName + " loaded from Classpath::XYZ = " + prop.getProperty("XYZ"));

    }

    private static void readPropertyFileFromClasspathWithClass(String propertyFileName) throws IOException {

        Properties prop = new Properties();
        prop.load(PropertyFilesUtil.class.getResourceAsStream(propertyFileName));
        System.out.println(propertyFileName + " loaded from Classpath::db.host = " + prop.getProperty("db.host"));
        System.out.println(propertyFileName + " loaded from Classpath::db.user = " + prop.getProperty("db.user"));
        System.out.println(propertyFileName + " loaded from Classpath::db.pwd = " + prop.getProperty("db.pwd"));
        System.out.println(propertyFileName + " loaded from Classpath::XYZ = " + prop.getProperty("XYZ"));

    }

    private static void readPropertyFileFromClasspathWithStream(String propertyFileName) throws IOException {

        Properties prop = new Properties();
        prop.load(new BufferedInputStream(new FileInputStream("D:/DB.properties")));
        System.out.println(propertyFileName + " loaded from Classpath::db.host = " + prop.getProperty("db.host"));
        System.out.println(propertyFileName + " loaded from Classpath::db.user = " + prop.getProperty("db.user"));
        System.out.println(propertyFileName + " loaded from Classpath::db.pwd = " + prop.getProperty("db.pwd"));
        System.out.println(propertyFileName + " loaded from Classpath::XYZ = " + prop.getProperty("XYZ"));

    }

    private static void readPropertyFileFromClasspathWithBundle(String propertyFileName) throws IOException {

        Locale locale = Locale.getDefault();
        ResourceBundle localResource = ResourceBundle.getBundle(
                        "DB", locale);// ResourceBundle这个类提供软件国际化的捷径
        // PropertyResourceBundle localResource = new PropertyResourceBundle(new BufferedInputStream(new FileInputStream("D:/DB.properties")));
        System.out.println(propertyFileName + " loaded from Classpath::db.host = " + localResource.getString("db.host"));
        System.out.println(propertyFileName + " loaded from Classpath::db.user = " + localResource.getString("db.user"));
        System.out.println(propertyFileName + " loaded from Classpath::db.pwd = " + localResource.getString("db.pwd"));

    }

    /**
     * read all the keys from the given property files
     * 
     * @param propertyFileName
     * @param xmlFileName
     * @throws IOException
     */
    private static void readAllKeys(String propertyFileName, String xmlFileName) throws IOException {

        System.out.println("Start of readAllKeys");
        Properties prop = new Properties();
        FileReader reader = new FileReader(propertyFileName);
        prop.load(reader);
        Set<Object> keys = prop.keySet();
        for ( Object obj : keys )
        {
            System.out.println(propertyFileName + ":: Key=" + obj.toString() + "::value=" + prop.getProperty(obj.toString()));
        }
        // loading xml file now, first clear existing properties
        prop.clear();
        InputStream is = new FileInputStream(xmlFileName);
        prop.loadFromXML(is);
        keys = prop.keySet();
        for ( Object obj : keys )
        {
            System.out.println(xmlFileName + ":: Key=" + obj.toString() + "::value=" + prop.getProperty(obj.toString()));
        }
        // Now free all the resources
        is.close();
        reader.close();
        System.out.println("End of readAllKeys");
    }

    /**
     * This method reads property files from file system
     * 
     * @param propertyFileName
     * @param xmlFileName
     * @throws IOException
     * @throws FileNotFoundException
     */
    private static void readPropertyFile(String propertyFileName, String xmlFileName) throws FileNotFoundException, IOException {

        System.out.println("Start to readPropertyFile");
        Properties prop = new Properties();
        FileReader reader = new FileReader(propertyFileName);
        prop.load(reader);
        System.out.println(propertyFileName + "::db.host = " + prop.getProperty("db.host"));
        System.out.println(propertyFileName + "::db.user = " + prop.getProperty("db.user"));
        System.out.println(propertyFileName + "::db.pwd = " + prop.getProperty("db.pwd"));
        System.out.println(propertyFileName + "::XYZ = " + prop.getProperty("XYZ"));
        // loading xml file now, first clear existing properties
        prop.clear();
        InputStream is = new FileInputStream(xmlFileName);
        prop.loadFromXML(is);
        System.out.println(xmlFileName + "::db.host = " + prop.getProperty("db.host"));
        System.out.println(xmlFileName + "::db.user = " + prop.getProperty("db.user"));
        System.out.println(xmlFileName + "::db.pwd = " + prop.getProperty("db.pwd"));
        System.out.println(xmlFileName + "::XYZ = " + prop.getProperty("XYZ"));
        // Now free all the resources
        is.close();
        reader.close();
        System.out.println("End of readPropertyFile");
    }

    /**
     * 两种格式写入属性文件,并将它存储在项目根目录中
     * 
     * @param fileName
     * @throws IOException
     */
    private static void writePropertyFile(String propertyFileName, String xmlFileName) throws IOException {

        System.out.println("Start to writePropertyFile");
        Properties prop = new Properties();
        prop.setProperty("db.host", "localhost");
        prop.setProperty("db.user", "user");
        prop.setProperty("db.pwd", "password");
        prop.store(new FileWriter(propertyFileName), "DB Config file");
        System.out.println(propertyFileName + " written successfully");
        prop.storeToXML(new FileOutputStream(xmlFileName), "DB Config XML file");
        System.out.println(xmlFileName + " written successfully");
        System.out.println("End of writePropertyFile");
    }

}

 

posted on 2016-11-23 15:16  我叫涛哥  阅读(140)  评论(0)    收藏  举报

导航