file路径正确但是找不到文件问题

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-28 02:20   11   0

//选择配置文件加载

public Properties getProp(String code){

try {

File file = new File(this.getClass().getClassLoader().getResource("/").toURI().getPath()+"res");

String[] fileName = file.list();

String[] fname = new String[fileName.length];

Properties prop = new Properties();

for (int i = 0; i < fileName.length; i++) {

fname[i] = fileName[i].substring(0,4);

if(code.substring(1).equals(fname[i])){

prop.load(this.getClass().getResourceAsStream("/res/"+fileName[i]));

}

}

return prop;

} catch (Exception e) {

e.printStackTrace();

returnnull;

}

}

问题是:

this.getClass().getClassLoader().getResource(“/”).getPath()+”res”

得到的路径如果有空格会把空格编译成%20,导致识别不了文件路径

解决办法:

getResource(“/”).toURI().getPath()

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP