java文件怎么获取项目下的配置文件
2个回答
展开全部
package utils;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
public class PropertieUtil {
Properties pro = null;
public PropertieUtil(){
this.pro = new Properties();
}
public Properties getProperties(){
return this.pro;
}
public Properties load(String fileName){
FileInputStream in = null;
try {
fileName = String.valueOf(PropertieUtil.class.getResource("/")).replace("file:/", "") + fileName;
in = new FileInputStream(fileName);
pro.load(in);
} catch (FileNotFoundException e) {
System.out.println("==================================");
System.out.println(fileName+"配置文件不存在,请联系管理员");
System.out.println("异常信息:"+e);
} catch (IOException e) {
System.out.println("==================================");
System.out.println("读取"+fileName+"配置文件时发生异常,请联系管理员");
System.out.println("异常信息:"+e);
}
return pro;
}
public static void main(String[] args) {
FileInputStream in = null;
System.out.println(String.valueOf(PropertieUtil.class.getResource("/")).replace("file:/", ""));
String fileName = String.valueOf(PropertieUtil.class.getResource("/")).replace("file:/", "") + "fileconfig.properties";
try {
in = new FileInputStream(fileName);
System.out.println(""+true);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
上面是读取.properties后缀的文件,你想要这种吗?
希望能够帮到你!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询