问一个读取属性文件的问题:
Test.properties文件有如下内容:
name valueadd AddCart想获取key为add的内容,即AddCart,方法如下
public class CommandProperties { private Properties pro;
public String getCommandName(String key){
InputStream fis=CommandProperties.class.getResourceAsStream("Test.properties");
pro=new Properties();
try {
pro.load(fis);
} catch (IOException e) {
e.printStackTrace();
}
return pro.getProperty(key);
}
}为什么测试中发现返回的是null?方法是在网上找的,求高人指点!
解决方案 »
- jsp错误提示
- 多个spring,applicationContext文件,在一个文件中,怎么引用其他文件中的bean
- htmlParser在JSP中应用中的一个错误
- 怎么办呀!点一个查询后。就内存溢出了
- 关于在web项目中的单元测试问题
- 100分!这个程序怎么了?晕
- 程序可以发布后所遇到的问题
- [求助]请问: 在asp里面的response.end 在jsp中对应什么呢?
- 调用getWriter之后试图调用getOutputStream怎么解决?
- redis异常JedisClusterMaxAttemptsException: No more cluster attempts left.
- 动态的饼状图
- quartz1.6自动调度每天某个时间段表达式
2.确保你的Test.properties在classpath下(src),如果不是的话需要添加完整包名。
public boolean loadPropFile(String sPropFileName) {
File file = new File(sPropFileName);
try {
FileInputStream in = new FileInputStream(file);
properties = new Properties();
properties.load(in);
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* Get property value
*
* @param key
* String : properties name
* @return String : properties value
*/ public String getPropValue(String key) { // If Key is null or equals blank, return "".
if (null == key || key.equals("")) {
return "";
} String value = getStr(properties.getProperty(key));
try {
// For Chinese word, double byte conversion.
value = new String(value.getBytes("8859_1"), "GB2312");
} catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
value = "";
}
return value; } /**
* String convertion function.
*
* @param pInStr
* the string to be converted
* @return the converted string
*/ private String getStr(String pInStr) {
return null == pInStr ? "" : pInStr.trim();
}用这个试试,还有就是注意文件路径。你那KEY有对应值吗.下班了,go........
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;public class CommandProperties {
private Properties pro; public String getCommandName(String key) {
InputStream fis;
try {
fis = new FileInputStream("Test.properties");
pro = new Properties();
pro.load(fis);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace(); } catch (IOException e) {
e.printStackTrace();
}
return pro.getProperty(key);
} public static void main(String[] args) {
String result = new CommandProperties().getCommandName("name");
System.out.println("result=" + result);
}}这个没有问题,我试过了,使用FileInputStream
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;public class CommandProperties {
private Properties pro; public String getCommandName(String key) {
InputStream fis;
try {
fis = new FileInputStream("config/Test.properties");
pro = new Properties();
pro.load(fis);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace(); } catch (IOException e) {
e.printStackTrace();
}
return pro.getProperty(key);
} public static void main(String[] args) {
String result = new CommandProperties().getCommandName("name");
System.out.println("result=" + result);
}}
这个没有问题,我试过了,使用FileInputStream