java读properties配置文件

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;

public class ConfigureProperties {
private static ConfigureProperties cp = null;
private String MediaServerPath = null;
private String MediaServicePyroProxyIP = null;
private String MediaServicePyroProxyPort = null;
private String propPath = null;
private Properties p = null;

///< 变量 get,set方法
public String getPropPath() {
return propPath;
}
public void setPropPath(String propPath) {
this.propPath = propPath;
}
public String getMediaServerPath() {
return MediaServerPath;
}
public void setMediaServerPath(String mediaServerPath) {
MediaServerPath = mediaServerPath;
}
public String getMediaServicePyroProxyIP() {
return MediaServicePyroProxyIP;
}
public void setMediaServicePyroProxyIP(String mediaServicePyroProxyIP) {
MediaServicePyroProxyIP = mediaServicePyroProxyIP;
}
public String getMediaServicePyroProxyPort() {
return MediaServicePyroProxyPort;
}
public void setMediaServicePyroProxyPort(String mediaServicePyroProxyPort) {
MediaServicePyroProxyPort = mediaServicePyroProxyPort;
}

///< 构造方法
private ConfigureProperties(){
URL basePath = this.getClass().getResource("/");
this.propPath = basePath.getPath() + "configuration.properties";//测试地址
p = new Properties();
try {
  FileInputStream fileInputStream = new FileInputStream( new File(this.propPath) );
  p.load(fileInputStream);
}
catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}

// 读取配置
read();
}

///< 单例
public static ConfigureProperties getInstance(){
if(cp == null)
cp = new ConfigureProperties();

return cp;
}

///< 读取配置
public boolean read(){

this.MediaServerPath = (String)p.get("MediaServerPath");
this.MediaServicePyroProxyIP = (String)p.get("MediaServicePyroProxy.IP");
this.MediaServicePyroProxyPort = (String)p.get("MediaServicePyroProxy.Port");

return true;
}

}




configuration.properties文件的内容为:
# MediaServer configuration
MediaServerPath=http://192.168.5.254/spots/all
MediaServicePyroProxy.IP=192.168.12.85
MediaServicePyroProxy.Port=6000


该配置文件一般放在src文件夹下
需要设置一下:
项目--“右键”点击“属性”--“java构建路径”--“排序和导出”中添加上该文件
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值