2016-12-08 90 views
1

我想先打印我的應用程序屬性到java中,然後將它推入thymeleaf html頁面。目的:允許用戶使用GET/POST編輯屬性文件。我當前的代碼將顯示屬性的值鍵和值到控制檯,如果它等於某事。我怎樣才能得到它只會提取特定的和多個前綴?如何通過前綴讀取應用程序屬性文件?

代碼/嘗試

public class ReadPropertiesFile { 


public static void readProp() { 

try { 
    Properties prop = new Properties(); 
    prop.load(ReadPropertiesFile.class.getResourceAsStream("/application.properties")); 

    Enumeration enuKeys = prop.keys(); 
     while (enuKeys.hasMoreElements()) { 
       String key = (String) enuKeys.nextElement(); 
       String value = prop.getProperty(key); 
       System.out.println(key + "= " + value); 
      } 
      } catch (FileNotFoundException e) { 
      //System.out.print("System cannot find file"); 
       e.printStackTrace(); 
      } catch (IOException e) { 
       //System.out.print("System cannot find file"); 
       e.printStackTrace(); 
      } 
} 

例爲application.properties

prefix.foo = [email protected][email protected][email protected]!scar 
prefix.cool = [email protected][email protected] 
some.feed = [email protected][email protected]!offline 
some.feed = [email protected][email protected]!cat 
noprefix = [email protected][email protected]!me 
host = [email protected][email protected]!host3 

爲了能夠只打印前綴和一些的所有值。

+0

你是什麼意思的多個前綴? – Coder

+0

也許你在問春季引導嗎?然後,我建議將這個標籤添加到問題中。 –

+0

@coder在application.properties中可以看到前綴。(name)或一些。(name)。我想編寫一個可以根據前綴提取這些鍵的java代碼。 – Jesse

回答

1
public class ReadPropertiesFile { 


public static void readProp() { 

try { 
    Properties prop = new Properties(); 
    prop.load(ReadPropertiesFile.class.getResourceAsStream("/application.properties")); 

    Enumeration enuKeys = prop.keys(); 
     while (enuKeys.hasMoreElements()) { 
       String key = (String) enuKeys.nextElement(); 
       String value = prop.getProperty(key); 
       if (key.startsWith("prefix") || key.startsWith("some")) { 
        System.out.println(key + "= " + value); 
       } 
      } 
      } catch (FileNotFoundException e) { 
      //System.out.print("System cannot find file"); 
       e.printStackTrace(); 
      } catch (IOException e) { 
       //System.out.print("System cannot find file"); 
       e.printStackTrace(); 
      } 
} 

這是你想要的嗎?只需打印以「前綴」或「某些」開頭的密鑰?

相關問題