//以下代码在读文件时,读到不到更新后的文件
public void test1() throws IOException{
ClassLoader loader = StudentDao.class.getClassLoader();
InputStream in = loader.getResourceAsStream("cn/itcast/dao/db.properties");
Properties prop = new Properties();
prop.load(in);
String url = prop.getProperty("url");
String username = prop.getProperty("username");
String password = prop.getProperty("password");
System.out.println(url);
System.out.println(username);
System.out.println(password);
}
这段代码为什么都不到更新后的文件
关于类加载器读配置文件的问题
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
hongyu83916 2017-05-06 23:52关注你这里用相对路径就可以了,看下你db.properties,假如你的文件在src下,就直接用db.properties。你打个断点看一下。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;/**
- 操作properties文件的工具类
- @author hongyu
*
/
public class PropertiesUtil {
/*
- 通过key取value
- @param key指定的key
- @return 与key对应的value */ public static int getValue(String key,String proFileName){ int value=0; String vTemp="0"; Properties pt=new Properties(); String path=PropertiesUtil.class.getResource("/").getPath(); try { pt.load(new FileInputStream(new File(path,proFileName))); vTemp=pt.getProperty(key); value=Integer.parseInt(vTemp); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return value; } }
解决 无用评论 打赏 举报