读取程序路径PropHelper工具类

package com.ving.dataexchange.util;

public class PropHelper {
/**
* guessPropFile:
*
* @param cls:和要寻找的属性文件处于相同的包中的任意的类
* @param propFile:要寻找的属性文件名
*/
public static java.io.InputStream guessPropFile(Class cls, String propFile) {
try {
// 得到类的类装载器
ClassLoader loader = cls.getClassLoader();

// 先从当前类所处路径的根目录中寻找属性文件
java.io.InputStream in = loader.getResourceAsStream(propFile);
if (in != null)
return in;

// 没有找到,就从该类所处的包目录中查找属性文件
Package pack = cls.getPackage();
if (pack != null) {
String packName = pack.getName();
String path = "";
if (packName.indexOf(".") < 0)
path = packName + "/";
else {
int start = 0, end = 0;
end = packName.indexOf(".");
while (end != -1) {
path = path + packName.substring(start, end) + "/";
start = end + 1;
end = packName.indexOf(".", start);
}
path = path + packName.substring(start) + "/";
}
in = loader.getResourceAsStream(path + propFile);
if (in != null)
return in;
}

// 如果没有找到,再从当前系统的用户目录中进行查找
java.io.File f = null;
String curDir = System.getProperty("user.dir");
f = new java.io.File(curDir, propFile);
if (f.exists())
return new java.io.FileInputStream(f);

// 如果还是没有找到,则从系统所有的类路径中查找
String classpath = System.getProperty("java.class.path");
String[] cps = classpath
.split(System.getProperty("path.separator"));

for (int i = 0; i < cps.length; i++) {
f = new java.io.File(cps[i], propFile);
if (f.exists())
break;
f = null;
}
if (f != null)
return new java.io.FileInputStream(f);
return null;
} catch (Exception e) {
throw new RuntimeException(e);
}

}
public static String guessPropFilePath(Class cls, String propFile) {
try {
// 得到类的类装载器
ClassLoader loader = cls.getClassLoader();

// 先从当前类所处路径的根目录中寻找属性文件
java.io.InputStream in = loader.getResourceAsStream(propFile);
if (in != null)
return propFile;

// 没有找到,就从该类所处的包目录中查找属性文件
Package pack = cls.getPackage();
if (pack != null) {
String packName = pack.getName();
String path = "";
if (packName.indexOf(".") < 0)
path = packName + "/";
else {
int start = 0, end = 0;
end = packName.indexOf(".");
while (end != -1) {
path = path + packName.substring(start, end) + "/";
start = end + 1;
end = packName.indexOf(".", start);
}
path = path + packName.substring(start) + "/";
}
in = loader.getResourceAsStream(path + propFile);
if (in != null)
return path;
}

// 如果没有找到,再从当前系统的用户目录中进行查找
java.io.File f = null;
String curDir = System.getProperty("user.dir");
f = new java.io.File(curDir, propFile);
if (f.exists())
return f.getAbsolutePath();

// 如果还是没有找到,则从系统所有的类路径中查找
String classpath = System.getProperty("java.class.path");
String[] cps = classpath
.split(System.getProperty("path.separator"));

for (int i = 0; i < cps.length; i++) {
f = new java.io.File(cps[i], propFile);
if (f.exists())
break;
f = null;
}
if (f != null)
return f.getAbsolutePath();
return null;
} catch (Exception e) {
throw new RuntimeException(e);
}

}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值