2011-05-17 78 views
1

我在default.properties(AndroidManifest.xml)旁邊定義了一個名爲my_config.properties的文件。我的問題是。如何在我的課堂上打開這個文件?android打開項目中的.properties文件

,如果我將它移動到類包,我可以使用如下因素代碼閱讀:

Properties configFile = new Properties(); 
    try { 
     configFile.load(MyConstantsClass.class.getResourceAsStream("my_config.properties")); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

但在這種情況下,文件需要與類我用這個片段在同一封裝內。當我的問題在開始時,我如何定義它? 謝謝。

回答

2

您只能從Android中打開包含在APK中的文件,my_config.properties的當前位置不會包含在那裏。我建議這種文件的正確位置是您的「資產」目錄,您必須使用the correct class才能訪問它。

+0

感謝。但有沒有什麼機會可以在構建時間內定義這樣的東西。我不需要apk中的這些屬性。我只需要根據產品/測試配置文件編譯應用程序。例如,我將有一個常量定義爲測試和錯誤作爲產品。我需要這個,因爲我爲生產和測試服務器使用了不同的基礎URL。有沒有一種方法可以比使用if子句更好地處理這種事情,在發佈之前手動檢查代碼以完成生產或.java類的所有更改? – DArkO 2011-05-17 11:33:04

+1

在這種情況下,如果你想知道你是否在生產或開發環境,那麼你最好看看這個問題:http://stackoverflow.com/questions/1743683/distinguishing-development-mode-and-release -mode-environment-settings-on-android否則我不知道你會怎麼做你想在Android上做什麼。 – 2011-05-17 12:32:48

+0

好的謝謝你的一切。 – DArkO 2011-05-18 07:42:45

0

你能夠獲得價值從的.properties文件的例子是以下:

Properties prop = new Properties(); 
    String propertiesPath = this.getFilesDir().getPath().toString() + "/app.properties"; 

    try { 

     File existFile = new File(propertiesPath); 

     if(existFile.isFile()) 
     { 
      FileInputStream inputStream = new FileInputStream(propertiesPath); 
      prop.load(inputStream); 
      inputStream.close();  
     } 

    } catch (IOException e) { 
     System.err.println("Failed to open app.properties file"); 
     e.printStackTrace(); 
    } 

Aey.Sakon

相關問題