JAva propertiesプロファイルの読み込み

3480 ワード

package com.xing.test;
import org.apache.commons.configuration2.PropertiesConfiguration; import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder; import org.apache.commons.configuration2.builder.fluent.Configurations; import java.io.*; import java.util.Properties; import java.util.PropertyResourceBundle; import java.util.ResourceBundle;
public class ReadProperties { public static void main(String[] args) throws IOException {
    String filePath = ReadProperties.class.getClassLoader().getResource("conf/demo.properties").getPath();

    /**    
     *    ClassLoader      
     *                  
     */
    Properties properties = new Properties();
    //           target/classes      
    InputStream in = ReadProperties.class.getClassLoader().getResourceAsStream("conf/demo.properties");
    properties.load(in);
    System.out.println("1111111111111---->:"+properties.getProperty("name"));


    /**    
     *    InputStream      
     *
     */
    Properties properties2 = new Properties();
    //              ,            
    //   BufferedReader     
    try (BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath))) {
        properties2.load(bufferedReader);
        System.out.println("22222222222---->:"+properties2.getProperty("name"));
    }catch (Exception e){
        e.printStackTrace();
    }
    //   FileInputStreamm     
    InputStream in2 = new FileInputStream(new File(filePath));
    properties2.load(in2);
    System.out.println("22222222222---->:"+properties2.getProperty("name"));



    /**    
     *    ResourceBundle      
     *
     */
    // 1.   ResourceBundle.getBundle()                       
    ResourceBundle resourceBundle = ResourceBundle.getBundle("conf/demo");
    System.out.println("333333333333----->:"+resourceBundle.getString("name"));
    // 2.   InputStream    
    InputStream in3 = new FileInputStream(new File(filePath));
    ResourceBundle resourceBundle2 = new PropertyResourceBundle(in3);
    System.out.println("333333333333----->:"+resourceBundle2.getString("name"));


    /**    
     *    PropertiesConfiguration                
     *
     */

    try {
        Configurations configurations = new Configurations();
        FileBasedConfigurationBuilder.setDefaultEncoding(PropertiesConfiguration.class, "UTF-8");
        PropertiesConfiguration propertiesConfiguration = configurations.properties(filePath);
        System.out.println("444444444444----->:"+propertiesConfiguration.getString("name"));


        //InputStream in4 = new FileInputStream(new File(filePath));
        PropertiesConfiguration propertiesConfiguration1 = new PropertiesConfiguration();
        propertiesConfiguration1.read(new BufferedReader(new FileReader(new File(filePath))));
        System.out.println("555555555555----->:"+propertiesConfiguration1.getString("name"));
    } catch (org.apache.commons.configuration2.ex.ConfigurationException e) {
        e.printStackTrace();
    }
}

}
Properties properties = new Properties (); try { properties = PropertiesLoaderUtils.loadAllProperties (“setting.properties”); System.out.println (new String (properties.getProperty (“warshipType.2”).getBytes (“iso-8859-1”), “gbk”)); } catch (IOException e) { e.printStackTrace (); }