2017-09-05 50 views
0

我想加载属性文件并读取属性文件中的给定键的值。属性文件看起来像:java属性不再加载输入流的相同的流再次

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 

我试图调试,为第一print语句在props.load加载所有3个属性为道具,但对于第二和第三打印语句props.load负荷为零性质为道具。

+0

第一props.load(流)是要读取文件的全部内容为对象的属性,并击中了文件的末尾,以便为属性对象第一个的getValue将有“A”,“B”和“C” ”。由于属性对象没有被返回,所以它被垃圾回收。下一次调用getValue会创建一个新的Properties对象,但是由于流已经到达文件的末尾,所以没有剩下任何东西读入它(以后的任何调用getValue都是一样的)。 – Sticks

+0

为什么你会这样做,而不是保存'Properties'对象? – EJP

回答

0

第一次读入属性文件后,FileInputStream到达其末尾,随后的读取将导致无数据,从而使新的Properties实例为空。您必须创建一个新的FileInputStream或(更好)一次读取和创建Properties,并将其用于getValue-调用。

0

提供的数据流位于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"));