很简单的代码:
/*
* Main.java
*
* Created on 2006年10月2日, 下午4:28
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/package javaapptest;import java.util.Properties;
import java.lang.reflect.*;
/**
*
* @author chen1
*/
public class Main {
private Properties properties = new Properties();
private final static String MAPPING_PROPERTIES = "mapping.properties";
/** Creates a new instance of Main */
public Main() {
try {
properties.load(getClass().getResourceAsStream(MAPPING_PROPERTIES));
} catch (java.io.IOException e) {}
}
public Properties getProperties(){
return properties;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Main main = new Main();
String h = null;
String w = null;
h = main.getProperties().getProperty("hello");
w = main.getProperties().getProperty("world");
System.out.println(h+w);
}
}
mapping.properties文件如下:
# Sample ResourceBundle properties file
hello=hello
world=world! 运行出现说load()NullPointer,我对Properties类读取properties属性文件不是很了解,哪位大虾指点一下,不胜感激。
/*
* Main.java
*
* Created on 2006年10月2日, 下午4:28
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/package javaapptest;import java.util.Properties;
import java.lang.reflect.*;
/**
*
* @author chen1
*/
public class Main {
private Properties properties = new Properties();
private final static String MAPPING_PROPERTIES = "mapping.properties";
/** Creates a new instance of Main */
public Main() {
try {
properties.load(getClass().getResourceAsStream(MAPPING_PROPERTIES));
} catch (java.io.IOException e) {}
}
public Properties getProperties(){
return properties;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Main main = new Main();
String h = null;
String w = null;
h = main.getProperties().getProperty("hello");
w = main.getProperties().getProperty("world");
System.out.println(h+w);
}
}
mapping.properties文件如下:
# Sample ResourceBundle properties file
hello=hello
world=world! 运行出现说load()NullPointer,我对Properties类读取properties属性文件不是很了解,哪位大虾指点一下,不胜感激。
解决方案 »
- Random类?
- "=="的疑惑
- Socket文件传输效率的问题
- 自以为是高手,初次接触java,是看了 java.lang.reflect.Proxy 的newProxyInstance方法 的源码30分钟后, 头晕了。
- 请教个算法,100分
- 请问java类里有没有一种函数把一个用逗号区分的String 变量以逗号为标记把数取出来?
- 请问socket类的用途?
- import问题
- 用socket发邮件的程序,怎么样才能发邮件的信箱中??
- 帮我看看怎么样引用没有打包的类
- java 关闭word文件时提示"此程序正由另一应用程序或用户使用.(c:\...\Normal.dot)"?
- 数组之间的类型转换
需要用/开头,也就是说从classpath的根开始寻找