java获取配置文件后的返回值问题,systemConfi该如何赋值
public class SystemConfigUtil
{
public static SystemConfig getSystemConfig()
{
InputStream inputStream = SystemConfigUtil.class.getClassLoader().getResourceAsStream("config.properties");
Properties p = new Properties();
try{
p.load(inputStream);
}catch(IOException e){
e.printStackTrace();
}
// System.out.println("FilePath"+p.getProperty("FilePath")+",ImagePath"+p.getProperty("ImagePath")+",PortraitPath"+p.getProperty("PortraitPath"));
return systemConfig;//该如何赋值....初学不是很懂
}
}
public class SystemConfigUtil
{
public static SystemConfig getSystemConfig()
{
InputStream inputStream = SystemConfigUtil.class.getClassLoader().getResourceAsStream("config.properties");
Properties p = new Properties();
try{
p.load(inputStream);
}catch(IOException e){
e.printStackTrace();
}
// System.out.println("FilePath"+p.getProperty("FilePath")+",ImagePath"+p.getProperty("ImagePath")+",PortraitPath"+p.getProperty("PortraitPath"));
return systemConfig;//该如何赋值....初学不是很懂
}
}
{
public static Properties getSystemConfig()
{
InputStream inputStream = SystemConfigUtil.class.getClassLoader().getResourceAsStream("config.properties");
Properties p = new Properties();
try{
p.load(inputStream);
}catch(IOException e){
e.printStackTrace();
}
// System.out.println("FilePath"+p.getProperty("FilePath")+",ImagePath"+p.getProperty("ImagePath")+",PortraitPath"+p.getProperty("PortraitPath"));
return p;//
}
}
systemConfig.setXxx(p.get("Xxx"));
String v = p.getProperty("你的key")
Properties p = new Properties();
SystemConfig systemConfig = new SystemConfig();
systemConfig.(p.getProperty("name"));
try{
p.load(inputStream);
}catch(IOException e){
e.printStackTrace();
import java.io.IOException;
import java.util.Properties;import org.omg.CORBA.portable.InputStream;/**
* 我猜测这是一个配置文件类
* @author qian.xu
*
*/
public class SystemConfigUtil
{
private static Properties p = null;
private static SystemConfigUtil systemConfigUtil = new SystemConfigUtil();
//这里用到了单例模式,所以私有化构造方法
private SystemConfigUtil(){
}
/**
* 对外公开的方法,是SystemConfigUtil获得配置文件
* @return systemConfigUtil
*/
public static SystemConfigUtil getSystemConfig()
{
InputStream inputStream = (InputStream) SystemConfigUtil.class.getClassLoader()
.getResourceAsStream("config.properties");
try{
if(systemConfigUtil.p==null){
systemConfigUtil.p.load(inputStream);//如果systemConfigUtil中p没有东西给他加上东西
//否则返回
}
}catch(IOException e){
e.printStackTrace();
}
// System.out.println("FilePath"+p.getProperty("FilePath")+",ImagePath"+p.getProperty("ImagePath")+",PortraitPath"+p.getProperty("PortraitPath"));
return systemConfigUtil;//
}
}
SystemConfigUtil systemConfigUtil = getSystemConfig();
就好了
调用配置文件中的内容时
systemConfigUtil.p.getProperty("XXX")
虽然我觉得你应该在SystemConfigUtil把配置文件p的东西取出来该给谁给谁
不过看你的需求是啥了
1.你在getSystemConfig()中new这么多对象不是每次执行都要new一遍?应该不用吧
2.systemConfig.(p.getProperty("name"))没有意义,Properties中存放的是键值对key=value
比如name = Tom,你想获得Tom的值,应该是String name = p.getProperty("name");而且应该放在
p.load(inputStream).否则p中是没东西的.
package com.federal.file.util;import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;import com.federal.file.bean.SystemConfig;
/**
*
* 读取properties文件
* ===================================================
*
* ===================================================
*/
public class SystemConfigUtil
{
private static Properties p = null;
private static SystemConfig systemConfig = new SystemConfig();
//这里用到了单例模式,所以私有化构造方法
private SystemConfig(){ //报错
}
public static SystemConfig getSystemConfig()
{
InputStream inputStream = (InputStream) SystemConfigUtil.class.getClassLoader().getResourceAsStream("config.properties");
try{
if(systemConfig.p==null){ //报错
systemConfig.p.load(inputStream);//报错
//否则返回
}
}catch(IOException e){
e.printStackTrace();
}
// System.out.println("FilePath"+p.getProperty("FilePath")+",ImagePath"+p.getProperty("ImagePath")+",PortraitPath"+p.getProperty("PortraitPath"));
return systemConfig;
}
要返回systenConfig 我改了下~三处报错~ 郁闷
import java.io.IOException;
import java.util.Properties;import org.omg.CORBA.portable.InputStream;/**
* 我猜测这是一个配置文件类
* @author qian.xu
*
*/
public class SystemConfigUtil
{
private static Properties p = null;
private static SystemConfigUtil systemConfigUtil = new SystemConfigUtil();
//这里用到了单例模式,所以私有化构造方法
private SystemConfigUtil(){
}
/**
* 对外公开的方法,是SystemConfigUtil获得配置文件
* @return systemConfigUtil
*/
public static SystemConfigUtil getSystemConfig()
{
InputStream inputStream = (InputStream) SystemConfigUtil.class.getClassLoader()
.getResourceAsStream("config.properties");
try{
if(systemConfigUtil.p==null){
systemConfigUtil.p.load(inputStream);//如果systemConfigUtil中p没有东西给他加上东西
//否则返回
}
}catch(IOException e){
e.printStackTrace();
}
// System.out.println("FilePath"+p.getProperty("FilePath")+",ImagePath"+p.getProperty("ImagePath")+",PortraitPath"+p.getProperty("PortraitPath"));
return systemConfigUtil;//
}
}
报错是因为你的构造函数名和类名不一致
SystemConfig和SystemConfigUtil
你再试试?
我在我环境上跑的,没有报错