2016-09-19 118 views
-1

我有3個屬性文件,如web1.properties,web2.properties,web3.properties。我想用單個屬性對象一次加載所有的屬性文件。如何使用java加載和讀取多個屬性文件

Properties prop = new Properties(); 
prop.load("web1.properties"); 
prop.load("web2.properties"); 
prop.load("web3.properties");` 

我試着加載多個屬性文件的下面的代碼。

public class ReadFileExtension { 
    static Properties prop = new Properties(); 
    public static void main(String[] args) throws IOException { 
     String projectFolder = System.getProperty("user.dir"); 
     File f1 = new File(projectFolder); 
     File[] listOfFiles = f1.listFiles(); 
     System.out.println("Length:"+listOfFiles.length); 
     for(int i=0;i<listOfFiles.length;i++){ 
      if (listOfFiles[i].isDirectory()) { 
       //System.out.println("File " + listOfFiles[i].getAbsolutePath()); 
       String prop1 = listOfFiles[i].getAbsolutePath(); 
       if(prop1.contains("properties")){ 
        System.out.println(prop1); 

        File folder = new File(prop1); 
        File[] listOfFiles1 = folder.listFiles(); 

         for (int j = 0; j < listOfFiles1.length; j++) { 
          if (listOfFiles1[j].isFile()) { 
          //System.out.println("Feature File: " + listOfFiles1[j].getAbsolutePath()); 
          //prop.load(new FileInputStream(listOfFiles1[j].getAbsolutePath())); 
          prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(listOfFiles1[j].getAbsolutePath())); 
          } 
         } 
       } 
       } 
     } 
     System.out.println("Prop 1: "+prop.getProperty("ApplicationURL")); 
     System.out.println("Prop 2: "+prop.getProperty("Application")); 
    } 
} 
+0

您可以使用[this]之類的東西(http://stackoverflow.com/questions/5093432/multiple-properties-files)。 – Abhishek

+1

如果你執行你發佈的代碼,你會得到什麼? –

+0

@Abhishek NullPointerException在該鏈接中運行代碼時。 – tsivarajan

回答

0

您可以使用此語法加載多個屬性。

Properties properties = new Properties(); 

properties.load(new FileInputStream("web1.properties")); 

Properties properties2 = new Properties(); 
properties2.load(new FileInputStream("web2.properties")); 

properties.putAll(properties2); 

閱讀屬性將是相同的。

+0

這是一個用於兩個屬性文件。我需要加載三個像這樣的屬性。 – tsivarajan

+0

只需添加代碼即可將第三個文件和屬性讀取到第一個對象 –

+0

請查看我的編輯。 – tsivarajan

1

我認爲阿布舍克提供的鏈接將會起作用。

我已經在我的本地機器上測試過它,並且可以正常工作。

代碼:

public class MultiplePropertyLoader { 

    private static Properties properties; 

    public static void main(String[] args) { 

     try { 
      properties = new Properties(); 
      properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("resources/SecurityKey.properties")); 
      properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("resources/SecurityKey1.properties")); 
      properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("resources/SecurityKey2.properties")); 

      System.out.println("::: Property File 1 Data :::"); 
      System.out.println(properties.getProperty("ENCRYPTION_KEY")); 
      System.out.println(properties.getProperty("USERNAME")); 
      System.out.println(properties.getProperty("PASSWORD")); 

      System.out.println("::: Property File 2 Data :::"); 
      System.out.println(properties.getProperty("ENCRYPTION_KEY1")); 
      System.out.println(properties.getProperty("USERNAME1")); 
      System.out.println(properties.getProperty("PASSWORD1")); 

      System.out.println("::: Property File 3 Data :::"); 
      System.out.println(properties.getProperty("ENCRYPTION_KEY2")); 
      System.out.println(properties.getProperty("USERNAME2")); 
      System.out.println(properties.getProperty("PASSWORD2")); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

請郵寄我,如果不工作。

這裏是一個my git repository URL與運行代碼和示例數據。 您可以克隆存儲庫並在本地計算機上運行,​​它將起作用。

+0

你好@tsivarajan如果這個答案解決了你的問題,那麼請接受答案。 –