2017-09-05 31 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"));