百度空间 | 百度首页 
               
 
查看文章
 
java读取properties 7种的方法
2007-11-14 17:22

package FileS;

import java.util.*;
import java.io.*;
import javax.servlet.*;
public class PropertiesT {

public static void main(String[] args) {
   /*
   * Properties pps=System.getProperties(); pps.list(System.out);
   * properties file read
   */
//(1)****************************************
//   InputStream in;
//   try {
//    in = new BufferedInputStream(new FileInputStream("d:/proTest.properties"));
//    Properties p = new Properties();
//      p.load(in);
//   
//   } catch (FileNotFoundException e1) {
//    // TODO Auto-generated catch block
//    e1.printStackTrace();
//   } catch (IOException e) {
//    // TODO Auto-generated catch block
//    e.printStackTrace();
//   }
//******************************************
  
//(2)******************************************
//   ResourceBundle rb = ResourceBundle.getBundle("d:/proTest.properties", Locale.getDefault());
//******************************************
  
//(3)******************************************
//   InputStream in;
//   try {
//    in = new BufferedInputStream(new FileInputStream("d:/proTest.properties"));
//    ResourceBundle rb = new PropertyResourceBundle(in);
//   } catch (FileNotFoundException e1) {
//    // TODO Auto-generated catch block
//    e1.printStackTrace();
//   } catch (IOException ex) {
//    ex.printStackTrace();
//   }
//******************************************
  
//(4)******************************************
//   InputStream in = JProperties.class.getResourceAsStream("d:/proTest.properties");
//   Properties p = new Properties();
//   try {
//    p.load(in);
//   } catch (IOException e1) {
//    // TODO Auto-generated catch block
//    e1.printStackTrace();
//   }
//******************************************
  
//(5)******************************************
//   InputStream in = JProperties.class.getClassLoader().getResourceAsStream("d:/proTest.properties");
//   Properties p = new Properties();
//   try {
//    p.load(in);
//   } catch (IOException e1) {
//    // TODO Auto-generated catch block
//    e1.printStackTrace();
//   }
//******************************************
  
//(6)******************************************
//   InputStream in = ClassLoader.getSystemResourceAsStream("d:/proTest.properties");
//   Properties p = new Properties();
//   try {
//    p.load(in);
//   } catch (IOException e1) {
//    // TODO Auto-generated catch block
//    e1.printStackTrace();
//   }
//******************************************
  
//(7)******************************************
//   InputStream in = context.getResourceAsStream("d:/proTest.properties");
//   Properties p = new Properties();
//   p.load(in);
//******************************************
   Properties pps = new Properties();
   try {
    pps.load(new FileInputStream("d:/proTest.properties"));
    Enumeration ENUM = pps.propertyNames();
    while (ENUM.hasMoreElements()) {
     String str = (String) ENUM.nextElement();
     String strValue = pps.getProperty(str);

     System.out.println(str + " = " + strValue);
    }

   } catch (FileNotFoundException e) {
    e.printStackTrace();
   } catch (IOException ex) {
    ex.printStackTrace();
   }
}

}


类别:java技巧类 | 添加到搜藏 | 浏览() | 评论 (0)
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2009 Baidu