【问题标题】:java project and external file fil.propertiesjava项目和外部文件fil.properties
【发布时间】:2017-05-19 12:25:50
【问题描述】:

我在启动 java 程序时尝试加载外部 file.properties,但这不会加载文件。 我的桌面上有这个程序和 file.properties。 这不是网络应用程序。

我还在电脑上将环境变量配置为类路径和路径,但是当我运行程序时它看不到它们

String filePath = "file.properties";
Properties data = new Properties();
    try{

        FileInputStream stream = new FileInputStream(new File(filePath));
        data.load(stream);
        execute(args,data);

    } catch (FileNotFoundException e) {
        e.printStackTrace();

这是错误:

java.io.FileNotFoundException: file.properties (Impossibile trovare il 文件规范。)在 java.io.FileInputStream.open(Native 方法)在 java.io.FileInputStream.(FileInputStream.java:137) 在 it.applications.AssCitRice.ParseXmlSct.(ParseXmlSct.java:42) 在 it.applications.AssCitRice.ParseXmlSct.main(ParseXmlSct.java:286) log4j:WARN 找不到记录器的附加程序 (它..applications.AssegniCitRicezionePresentazione.ParseXmlSct)。 log4j:WARN 请正确初始化log4j系统。

请帮帮我

【问题讨论】:

  • FileNotFoundException。请检查filePath,好像不正确?
  • 是的,路径是正确的.. .:(
  • 我假设 ParseXmlSct 是您的班级名称?该文件中的第 42 行是什么?
  • 是的 ParseXmlSct 是我的班级名称
  • 那个类文件的第 42 行是什么?

标签: java properties load external


【解决方案1】:

您必须使用正确的(完整)路径名,例如:

String filePath = APLHOME + File.separator + "file.properties";
or
String filePath = System.getenv("APLHOME") + File.separator + "file.properties"; 

没有打开文本文件以查看类路径的机制

【讨论】:

    【解决方案2】:

    FileNotFoundException 基本上只是意味着您的文件不存在或您提供了错误的路径。

    您需要提供绝对路径。否则 Java 将无法找到它。

    //The below will work great as long as the file exists in that path.
    FileInputStream stream = new FileInputStream(new File("C:/test.txt"));
    
    //This will however not work
    FileInputStream stream = new FileInputStream(new File("/test.txt"));
    

    【讨论】:

      猜你喜欢
      • 2016-11-09
      • 1970-01-01
      • 2013-12-29
      • 2016-04-15
      • 2021-12-02
      • 1970-01-01
      • 2021-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多