當前位置:首頁 » 編程語言 » java讀取配置文件properties

java讀取配置文件properties

發布時間: 2023-09-10 02:18:36

java中怎麼讀取properties文件

(1)load(InputStream inStream)
這個方法可以從.properties屬性文件對應的文件輸入流中,載入屬性列表到Properties類對象。如下面的代碼:
Properties pro = new Properties();
FileInputStream in = new FileInputStream("a.properties");
pro.load(in);
in.close();

(2)store(OutputStream out, String comments)
這個方法將Properties類對象的屬性列表保存到輸出流中。如下面的代碼:
FileOutputStream oFile = new FileOutputStream(file, "a.properties");
pro.store(oFile, "Comment");
oFile.close();

② JAVA中如何讀取src下所有的properties文件

1.使用java.util.Properties類的load()方法


示例:

//文件在項目下。不是在包下!!

InputStream in = new BufferedInputStream(new FileInputStream("demo.properties")) ;

Properties p = new Properties();

p.load(in) ;

String className2 = p.getProperty("database.driver");

String url = p.getProperty("database.url");

String user = p.getProperty("database.user");

String password = p.getProperty("database.pass");

總結:如果是 在WEB上讀取properties文件,寫成下面這種。上面寫的那些只在 JavaSE 中

String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();

System.out.println(path);

InputStream in = new FileInputStream(new File(path+File.separator+"mysql.properties"));

Properties prop = new Properties();

熱點內容
java返回this 發布:2025-10-20 08:28:16 瀏覽:576
製作腳本網站 發布:2025-10-20 08:17:34 瀏覽:867
python中的init方法 發布:2025-10-20 08:17:33 瀏覽:565
圖案密碼什麼意思 發布:2025-10-20 08:16:56 瀏覽:747
怎麼清理微信視頻緩存 發布:2025-10-20 08:12:37 瀏覽:667
c語言編譯器怎麼看執行過程 發布:2025-10-20 08:00:32 瀏覽:988
郵箱如何填寫發信伺服器 發布:2025-10-20 07:45:27 瀏覽:236
shell腳本入門案例 發布:2025-10-20 07:44:45 瀏覽:96
怎麼上傳照片瀏覽上傳 發布:2025-10-20 07:44:03 瀏覽:789
python股票數據獲取 發布:2025-10-20 07:39:44 瀏覽:695