【问题标题】:how to read file.properties如何读取 file.properties
【发布时间】:2012-09-18 11:02:34
【问题描述】:

我有一个这样组织的项目:

-LIBRERIA CLIENT/
   -SRC/
     -CONNECTION/
      CONNECTIONdB.JAVA
      CONFIG.PROPERTIES

CONNECETIONDB 我有这段代码可以读取CONFIG.PROPERTIES 文件:

public static void connect() throws RemoteException
{
        //read file config.properties:
        String dbHost="",dbUser="",dbPassword="";
        Properties prop=new Properties();
        try
        {
            //load il file:
            prop.load(new FileInputStream("src/Connectionconfig.properties"));
            //read the property of file
            dbHost=prop.getProperty("host");
            dbUser=prop.getProperty("user");
            dbPassword=prop.getProperty("password");
        }catch(FileNotFoundException fe){
            System.out.println("File not found");
        }catch(IOException ex){
            System.out.println("Error reading configuration file");
        }

但我有这个错误:

     FILE NOT FOUND 

谁能告诉我哪里出错了?

【问题讨论】:

  • 无需大喊。所有大写都被认为是大喊大叫和粗鲁。
  • 大量使用大写锁定对您没有帮助...
  • 您认为“找不到文件”是什么意思?
  • 你为什么大喊大叫?如果未找到该文件,则表示该文件不在您尝试加载它的位置。
  • 如果应用程序在 Web 应用程序上下文中读取,这篇文章可能会对您有所帮助 stackoverflow.com/questions/2308188/…

标签: java properties


【解决方案1】:

这很简单:您的代码在 project_root 的不同文件夹中执行。此外:src/Connectionconfig.properties 不存在:它应该是SRC/CONNECTION/CONFIG.PROPERTIES,如果您从 project_root 执行项目。
还可以查看这个以获取当前工作目录:Getting the Current Working Directory in Java

【讨论】:

    【解决方案2】:

    您的代码读取了src 下的Connectionconfig.properties 文件,但根据您的项目结构,SRC/CONNECTION 下仅存在CONFIG.PROPERTIES。 改为

      prop.load(new FileInputStream("SRC/CONNECTION/CONFIG.PROPERTIES"));
    

    而不是

      prop.load(new FileInputStream("src/Connectionconfig.properties"));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-03
      • 2016-03-07
      • 2022-12-12
      • 1970-01-01
      相关资源
      最近更新 更多