Java属性未加载

时间:2013-06-24 00:46:26

标签: java properties io

我正在尝试在打包的jar之外设置一个java .properties文件。这是我加载它的代码:

public static final String FILENAME = "test.properties";

public static void load() throws IOException {
    FileInputStream fis = null;
    try {
        props = new Properties();
        fis = new FileInputStream(FILENAME);
        props.load(fis);
        System.out.println("Properties successfully loaded: "+props);
        validateProperties();
    } catch (FileNotFoundException e) {
        System.err.println("Properties file not found.  Creating...");
        new File(FILENAME).createNewFile();
                    //fill with default properties
        System.out.println("Properties file successfully created");
    } finally {
        if (fis != null) try {fis.close();} catch(Exception e) {}
    }
}

不幸的是,当我运行它时,我得到以下输出:

Properties successfully loaded: {}

这是test.properties:

#no comment
#Sun Jun 23 19:21:45 CDT 2013
port=55142
handSize=10
maxPlayers=8
timeout=1500

我已通过手动读取和打印确认FileInputStream正在从正确的文件中读取。那么为什么不加载我的属性呢?

编辑:这是一些直接加载属性文件内容的代码:

public static void test() throws IOException {
    FileInputStream fis = new FileInputStream(FILENAME);
    byte[] b = new byte[fis.available()];
    fis.read(b);
    String text = new String(b);
    System.out.println(text);
}

并输出:

#no comment
#Sun Jun 23 19:21:45 CDT 2013
port=55142
handSize=10
maxPlayers=8
timeout=500

所以FIS必须从正确的文件中读取。

编辑2:好的,所以我不知道问题是什么,但我重启了eclipse,现在它正在运行。很抱歉浪费了你的时间。

4 个答案:

答案 0 :(得分:0)

由于您的属性文件不存在于类路径中,因此如果不提供正确的路径,则无法读取它。有多种方法可以从jar中读取外部属性文件。最简单的方法之一是使用-D开关在java命令行上定义系统属性。该系统属性可能包含属性文件的路径。

E.g

java -cp ... -Dmy.app.properties=/path/to/test.properties my.package.App

然后,在您的代码中,您可以执行以下操作:

public static final String FILENAME = "test.properties";

public static void load() throws IOException {
    FileInputStream fis = null;
    String propPath = System.getProperty(FILENAME);

    try {
        props = new Properties();
        fis = new FileInputStream(propPath);
        props.load(fis);
        System.out.println("Properties successfully loaded: "+props);
        validateProperties();
    } catch (FileNotFoundException e) {
        System.err.println("Properties file not found.  Creating...");
        new File(propPath ).createNewFile();
                    //fill with default properties
        System.out.println("Properties file successfully created");
    } finally {
        if (fis != null) try {fis.close();} catch(Exception e) {}
    }
}

答案 1 :(得分:0)

当没有提到绝对路径时,JVM会尝试从JVM& project classpath。在您的情况下,空输出表示JVM正在尝试从classpath加载属性文件,但该文件不存在。

<强>解决方案:

1)将属性文件放在类路径中

2)或者提及属性文件的绝对路径。

答案 2 :(得分:0)

检查java系统使用的行分隔符。例如:

System.out.println((int)System.getProperty("line.separator").charAt(0));

在UNIX上将提供10,即新行\n,在Windows上为13(例如:\r\n的第一个字符。)

我认为您的java代码是使用Windows编码读取文件,但属性文件是在UNIX中编辑的,因此每个都显示在“单行”中 - 这将导致空属性,因为您的第一行被注释

答案 3 :(得分:0)

ResourceBundle提供了一种非常简单的方法来访问Java中属性文件中的键/值对... 您可以参考以下内容。

http://www.avajava.com/tutorials/lessons/how-do-i-read-a-properties-file-with-a-resource-bundle.html

您可以在加载捆绑包时直接指定属性文件名,当它与jar / classes位于同一文件夹中时。

相关问题