问一个关于properties文件的路径的问题

PoeLeon 2010-03-03 04:32:56
public static Connection getConnection() throws SQLException, IOException {

Properties props = new Properties();

String fileName = "GetDb.properties";

FileInputStream in = new FileInputStream(fileName);

props.load(in);



请问各位大侠,ViewDB.properties这个文件应该放在哪个路径下面呢?我试了放在SRC下面不行,总是

提示该文件无法找到,谢谢了。
...全文
117 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
shishan3595 2010-03-19
  • 打赏
  • 举报
回复
放在与src同级的目录下就行了,即工程路径下
liwis521125 2010-03-19
  • 打赏
  • 举报
回复
放在工程下就好了~~~
  • 打赏
  • 举报
回复
放在 src 下:

InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName)
props.load(is);
PoeLeon 2010-03-03
  • 打赏
  • 举报
回复
嗯,我用绝对路径可以,多谢大家。

wrong1111 2010-03-03
  • 打赏
  • 举报
回复
引用 5 楼 xiaoniqiu 的回复:
照你现在
public static Connection getConnection() throws SQLException, IOException {
   
    Properties props = new Properties();
   
    String fileName = "GetDb.properties";
   
    FileInputStream in = new FileInputStream(fileName);
   
    props.load(in);
这么写的话,文件应该放到class文件同级目录才行吧?要么就像楼上写绝对路径去


嗯。这么写才靠谱!
  • 打赏
  • 举报
回复
照你现在
public static Connection getConnection() throws SQLException, IOException {

Properties props = new Properties();

String fileName = "GetDb.properties";

FileInputStream in = new FileInputStream(fileName);

props.load(in);
这么写的话,文件应该放到class文件同级目录才行吧?要么就像楼上写绝对路径去
ychzxx 2010-03-03
  • 打赏
  • 举报
回复
String fileName ="C:/GetDb.properties";//这里可以为绝对路径
try
{
props.load(new FileInputStream(fileName ));
}
catch (Exception ce)
{
ce.printStackTrace();
}
gloomyfish 2010-03-03
  • 打赏
  • 举报
回复
put your property file into your package
-com.your.pkg
-your.java
-xxx.properties
your.class.getResourceAsStream("xxx.properties");
LuffySY 2010-03-03
  • 打赏
  • 举报
回复
把路径写清楚,放哪都可以
LuffySY 2010-03-03
  • 打赏
  • 举报
回复
String fileName = "GetDb.properties";
到底是ViewDB.properties还是GetDb.properties

62,624

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧