java属性不会再次加载同一流的输入流

标签 java properties

我正在尝试加载属性文件并读取属性文件中给定键的值。属性文件如下所示:

text.properties 
A=Z
B=Y
C=X

public class TestStreams {
static String path = "test.properties";

public static void main(String[] args) throws IOException {
    TestStreams test = new TestStreams();
    InputStream stream =  new FileInputStream(new File(path));

    System.out.println(test.getValue(stream, "A"));
    System.out.println(test.getValue(stream, "B"));
    System.out.println(test.getValue(stream, "C"));
}

public String getValue(InputStream stream, String key) throws IOException {
    Properties props = new Properties();
    String value = null;
    try {
        props.load(stream);
        value = props.getProperty(key);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return value;
   }
}

Output : 
Z
null
null

我尝试调试,对于 props.load 处的第一个打印语句将所有 3 个属性加载到 props 中,但对于第二个和第三个打印语句 props.load 将零个属性加载到 props 中。

最佳答案

提供的流位于 props.load 之后的结束位置,因此无法再读取任何内容。

您可以在每次调用之前重新打开流,或者(更好)只加载一次属性并重用 Properties 实例:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class TestStreams {
    static String path = "test.properties";

    public static void main(String[] args) throws IOException {
        TestStreams test = new TestStreams();
        InputStream stream = new FileInputStream(new File(path));

        System.out.println(test.getValue(stream, "A"));
        System.out.println(test.getValue(stream, "B"));
        System.out.println(test.getValue(stream, "C"));

        // Variant 1
        stream = new FileInputStream(new File(path));
        System.out.println(test.getValue(stream, "A"));
        stream = new FileInputStream(new File(path));
        System.out.println(test.getValue(stream, "B"));
        stream = new FileInputStream(new File(path));
        System.out.println(test.getValue(stream, "C"));

        // Variant 2
        stream = new FileInputStream(new File(path));
        Properties props = new Properties();
        props.load(stream);
        System.out.println(props.getProperty("A"));
        System.out.println(props.getProperty("B"));
        System.out.println(props.getProperty("C"));
    }

    public String getValue(InputStream stream, String key) throws IOException {
        Properties props = new Properties();
        String value = null;
        try {
            props.load(stream);
            value = props.getProperty(key);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return value;
    }
}

作为旁注,您应该使用 try-with-resources声明以确保您的流随后关闭:

Properties props = new Properties();
try (InputStream stream = new FileInputStream(new File(path));) {
    props.load(stream);
}
System.out.println(props.getProperty("A"));
System.out.println(props.getProperty("B"));
System.out.println(props.getProperty("C"));

关于java属性不会再次加载同一流的输入流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46062176/

相关文章:

c# - 将私有(private)成员封装为属性与定义没有私有(private)成员的属性有什么区别?

python - 理解 Python 中的属性

objective-c - Objective-C对象 Release模式

java - 如果您扩展了一个实现 Serializable "down the line"的类,为什么需要重新定义 serialVersionUID?

java - 为什么 Gradle 并不总是满足约束请求?

java - 耶拿眼球 validator : Scheme should be lower case

json - 如何从 HTTP 请求中选择 soapui 属性传输中的 json 值?

Python:用属性覆盖成员?

java - java中HashMap.containsKey()的时间复杂度是多少?

java - 当 View 在 ListView 中变为回收时,如何知道何时取消异步任务