返回值为空,读不到配置文件

cass_of_kiss 2011-11-05 04:30:08
public static String read(String fileName,String key) {
FileInputStream inputFile = null;
String filePath = null;
Properties pro = new Properties();
try {
filePath = new String(URLDecoder.decode(PropertyUtil.class
.getClassLoader().getResource(fileName).getPath(),
"UTF-8"));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("-----------"+filePath);
try {
String value = "";
inputFile = new FileInputStream(filePath);
pro.load(inputFile);

inputFile.close();
if(pro.contains(key)){
value = pro.getProperty(key);
return value;
}else{
return "";
}
} catch (FileNotFoundException e) {
e.printStackTrace();
return "";
} catch (IOException e) {
e.printStackTrace();
return "";
} catch (Exception e) {
e.printStackTrace();
return "";
}
}

求大虾们帮忙看看那。。。
...全文
67 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiang15101135905 2011-11-05
  • 打赏
  • 举报
回复
package com.accp.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertyUtil {

public static void main(String[] args){
System.out.println(read("aa.properties","aa"));
}

public static String read(String fileName, String key) {
InputStream inputFile = null;
String filePath = null;
Properties pro = new Properties();
try {
filePath = PropertyUtil.class.getResource(fileName).getPath();
System.out.println("-----------" + filePath);
inputFile = new FileInputStream(new File(PropertyUtil.class.getResource(fileName).getPath()));
} catch (Exception e1) {
e1.printStackTrace();
}
try {
String value = "";
pro.load(inputFile);
inputFile.close();
if (pro.containsKey(key)) {
value = pro.getProperty(key);
return value;
} else {
return "";
}
} catch (FileNotFoundException e) {
e.printStackTrace();
return "";
} catch (IOException e) {
e.printStackTrace();
return "";
} catch (Exception e) {
e.printStackTrace();
return "";
}
}

}
什么都不能 2011-11-05
  • 打赏
  • 举报
回复
ResourceBundle bundle = ResourceBunder.getBundle("context-adms");
String ipLibPath = bundle.getString("adms.iplib.path");
KG071 2011-11-05
  • 打赏
  • 举报
回复
不小心看错了 应该是
System.out.println(new File(fileName).getAbsolutePath());
KG071 2011-11-05
  • 打赏
  • 举报
回复
debug 最简单了

或在
filePath = new String(URLDecoder.decode(PropertyUtil.class.getClassLoader().getResource(fileName).getPath(),"UTF-8"));的前面加一句:
System.out.println(fileName.getAbsolutePath());

看看fileName的绝对路径对不对 如果不对 照着改就行了
jiang15101135905 2011-11-05
  • 打赏
  • 举报
回复
package com.accp.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertyUtil {

public static void main(String[] args){
System.out.println(read("aa.properties","aa"));
}

public static String read(String fileName, String key) {
InputStream inputFile = null;
String filePath = null;
Properties pro = new Properties();
try {
filePath = PropertyUtil.class.getResource(fileName).getPath();
System.out.println("-----------" + filePath);
inputFile = new FileInputStream(new File(PropertyUtil.class.getResource(fileName).getPath()));
} catch (Exception e1) {
e1.printStackTrace();
}
try {
String value = "";
pro.load(inputFile);
inputFile.close();
if (pro.containsKey(key)) {
value = pro.getProperty(key);
return value;
} else {
return "";
}
} catch (FileNotFoundException e) {
e.printStackTrace();
return "";
} catch (IOException e) {
e.printStackTrace();
return "";
} catch (Exception e) {
e.printStackTrace();
return "";
}
}

}
jiang15101135905 2011-11-05
  • 打赏
  • 举报
回复
应该在前边加个“/”才行

67,514

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧