簡體   English   中英

如何在Eclipse中的Java項目中添加Java Properties文件

[英]How to add a Java Properties file to my Java Project in Eclipse

我之前使用Unix來編譯和編輯我的Java。 我已經在我當前工作目錄中使用了屬性文件,其中存在類文件。 現在我已切換到Eclipse IDE。 我不知道如何在Eclipse中添加相同的屬性文件。 請幫我。

  1. 如果您的項目沒有,請在Java Resources文件夾下創建文件夾“resources”。
    1. 使用以下值創建config.properties文件。

在此輸入圖像描述

/ Java Resources / resources / config.properties

用於加載屬性。

Properties prop = new Properties();
InputStream input = null;

try {

      input = getClass().getClassLoader().getResourceAsStream("config.properties");


    // load a properties file
    prop.load(input);

    // get the property value and print it out
    System.out.println(prop.getProperty("database"));
    System.out.println(prop.getProperty("dbuser"));
    System.out.println(prop.getProperty("dbpassword"));

} catch (IOException ex) {
    ex.printStackTrace();
} finally {
    if (input != null) {
        try {
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

在包資源管理器中,右鍵單擊該包並選擇New - > File,然后輸入包含“.properties”后綴的文件名。

如果在當前工作目錄中有屬性文件,它應該在Unix中正常工作。 另一個選擇是將屬性文件添加到類路徑並使用this.getClass().getClassLoader().getResourceAsStream("xxxxx.properties");獲取輸入流this.getClass().getClassLoader().getResourceAsStream("xxxxx.properties"); 更多這里

腳步:

  1. 右鍵單擊要創建.properties文件的任何包,或根據需要創建新包
  2. 現在選擇new然后選擇文件(如果你找不到文件然后轉到你的包的位置並在那里創建它)
  3. 現在將其命名為yourfilename.properties

如果您使用'from existing source'選項在eclipse中創建了Java Project,那么它應該像以前一樣工作。 更准確地說File> New Java Project 在“內容”部分中,選擇“ 從現有源創建項目 ”,然后選擇現有項目文件夾。 向導將負責其余的工作。

  1. 在eclipse中右鍵單擊項目中要創建屬性文件的文件夾
  2. 在搜索過濾器類型文件和連續窗口中的New-> Other->給出擴展名為.properties的文件的名稱

要創建屬性類,請選擇要在其中創建屬性文件的包。

右鍵單擊包並選擇其他。 現在選擇File並使用(.properties)后綴鍵入文件名。 例如:db.properties。 比點擊完成。 現在,您可以在此屬性文件中編寫代碼。

如果您正在使用核心java,請通過右鍵單擊項目來創建文件( properties )。 如果文件存在於包或src文件夾中,則會拋出找不到文件的錯誤

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM