[英]How to add a Java Properties file to my Java Project in Eclipse
我之前使用Unix來編譯和編輯我的Java。 我已經在我當前工作目錄中使用了屬性文件,其中存在類文件。 現在我已切換到Eclipse IDE。 我不知道如何在Eclipse中添加相同的屬性文件。 請幫我。
/ 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");
更多這里
腳步:
如果您使用'from existing source'選項在eclipse中創建了Java Project,那么它應該像以前一樣工作。 更准確地說File> New Java Project 。 在“內容”部分中,選擇“ 從現有源創建項目 ”,然后選擇現有項目文件夾。 向導將負責其余的工作。
要創建屬性類,請選擇要在其中創建屬性文件的包。
右鍵單擊包並選擇其他。 現在選擇File並使用(.properties)后綴鍵入文件名。 例如:db.properties。 比點擊完成。 現在,您可以在此屬性文件中編寫代碼。
如果您正在使用核心java,請通過右鍵單擊項目來創建文件( properties
)。 如果文件存在於包或src文件夾中,則會拋出找不到文件的錯誤
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.