1 import java.io.FileNotFoundException;
2 import java.io.IOException;
3 import java.io.InputStream;
4 import java.util.Properties;
5
6 public class GetProperties {//不用static
7 public String getCotent(){//不用static
8 String content=”";
9
10 try {
11 Properties properties = new Properties();
12
13 InputStream is = getClass().getResourceAsStream(“test.properties”);//ok
14 //InputStream is = getClass().getClassLoader().getResourceAsStream(“test.properties”); //ERROR:Exception in thread “main” java.lang.NullPointerException
15
16 properties.load(is);
17 is.close();
18
19 content = properties.getProperty(“str1″);
20
21 } catch (FileNotFoundException ex) {
22 ex.printStackTrace();
23 }catch (IOException ex) {
24 ex.printStackTrace();
25 }
26 return content;
27 }
28
29 public static void main(String[] args){
30 GetProperties gp = new GetProperties();//实例化
31 String s = gp.getCotent();
32
33 System.out.println(s);
34 }
35 }
36
37 test.properties中内容为str1=123