请问如何在一个JAVA文件中读去配置文件(.properties)中的内容?配置文件的目录应该放在何处可直接用,配置文件的内容该怎么写?下面一个简单的程序?好象是不能读出配置文件的内容,请问错在哪儿啊?
package com;import java.util.Set;
import java.util.Iterator;
import java.util.Properties;import java.io.IOException;
import java.io.InputStream;public class PropertiesTest { public static void main(String args[]) { PropertiesTest test = new PropertiesTest();
InputStream in = test.getClass().getResourceAsStream("/a.properties");
       
Properties props = new Properties();
try {
props.load(in);
System.out.println("IN");
} catch (IOException e) 
{  
e.printStackTrace();

} System.out.println(props.getProperty("key_name")); Set set = props.keySet();
Iterator it = set.iterator(); System.out.println("Begin ...");
while (it.hasNext()) {
System.out.println((String) it.next());
}
System.out.println("End");
}}
配置文件名为a.properties,内容为:key_name="abcde";
怎么在程序中读不出来呢?

解决方案 »

  1.   

    给你个读取文本文件的例子,希望对你有帮助:import java.io.*;
    public class ReadFile {
      private File fileIn;
      private BufferedReader bRead;  public ReadFile() {
        fileIn = new File("C:/1.txt");
        try {
          bRead = new BufferedReader(new FileReader(fileIn));
        }
        catch (FileNotFoundException ex) {
        }
        String value = new String();
        try {
          while((value = bRead.readLine()) != null){
            System.out.println(value);
          }
        }
        catch (IOException ex1) {
        }
      }  public static void main(String[] args) {
        new ReadFile();
      }}
      

  2.   

    getResource(.. ) 得到的是class文件的根目录,比如bin/