1.Properties與ResourceBundle
兩個類別都可以讀取屬性檔案中以key/value形式儲存的鍵值對,ResourceBundle讀取屬性檔時操作相對簡單。
2.Properties
此類別繼承Hashtable,將鍵值對儲存在集合中。基於輸入流從屬性檔讀取鍵值對,load()方法調用完畢,就與輸入流脫離關係,不會自動關閉輸入流,需要手動關閉。
/** * 基于输入流读取属性文件:Properties继承了Hashtable,底层将key/value键值对存储在集合中, * 通过put方法可以向集合中添加键值对或者修改key对应的value * * @throws IOException */@SuppressWarnings("rawtypes") @Testpublic void test01() throws IOException { FileInputStream fis = new FileInputStream("Files/test01.properties"); Properties props = new Properties(); props.load(fis);// 将文件的全部内容读取到内存中,输入流到达结尾fis.close();// 加载完毕,就不再使用输入流,程序未主动关闭,需要手动关闭/*byte[] buf = new byte[1024]; int length = fis.read(buf); System.out.println("content=" + new String(buf, 0, length));//抛出StringIndexOutOfBoundsException*/System.out.println("driver=" + props.getProperty("jdbc.driver")); System.out.println("url=" + props.getProperty("jdbc.url")); System.out.println("username=" + props.getProperty("jdbc.username")); System.out.println("password=" + props.getProperty("jdbc.password"));/** * Properties其他可能用到的方法 */props.put("serverTimezone", "UTC");// 底层通过hashtable.put(key,value)props.put("jdbc.password", "456"); FileOutputStream fos = new FileOutputStream("Files/test02.xml");// 将Hashtable中的数据写入xml文件中props.storeToXML(fos, "来自属性文件的数据库连接四要素"); System.out.println(); System.out.println("遍历属性文件"); System.out.println("hashtable中键值对数目=" + props.size()); Enumeration keys = props.propertyNames();while (keys.hasMoreElements()) { String key = (String) keys.nextElement(); System.out.println(key + "=" + props.getProperty(key)); } }
3.ResourceBundle
該類別基於類別讀取屬性檔案:將屬性檔案當作類,意味著屬性檔案必須放在套件中,使用屬性檔案的全限定性類別名稱而非路徑指涉屬性檔案。
/** * 基于类读取属性文件:该方法将属性文件当作类来处理,属性文件放在包中,使用属性文件的全限定性而非路径来指代文件 */@Testpublic void test02() { ResourceBundle bundle = ResourceBundle.getBundle("com.javase.properties.test01"); System.out.println("获取指定key的值"); System.out.println("driver=" + bundle.getString("jdbc.driver")); System.out.println("url=" + bundle.getString("jdbc.url")); System.out.println("username=" + bundle.getString("jdbc.username")); System.out.println("password=" + bundle.getString("jdbc.password")); System.out.println("-----------------------------"); System.out.println("遍历属性文件"); Enumeration<string> keys = bundle.getKeys();while (keys.hasMoreElements()) { String key = keys.nextElement(); System.out.println(key + "=" + bundle.getString(key)); } }</string>
4.讀取屬性檔案到Spring容器
通常將資料庫連接四要素放在屬性文件中,程式從屬性檔讀取參數,這樣當資料庫連線要素改變時,就不需要修改原始碼。將屬性檔案中的內容載入到xml文件中的方法:
在設定檔頭配置context約束。
在設定檔中加入
,載入屬性到設定檔中。 取得設定檔內容:${key}
5.註解
#放在前面,用於在屬性檔案中新增註解。
6.編碼
屬性檔採用ISO-8859-1編碼方式,該編碼方式不支援中文,中文字元將會轉化為Unicode編碼方式顯示。
以上是Properties與ResourceBundle的詳細介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

JavadevelovermentIrelyPlatForm-DeTueTososeVeralFactors.1)JVMVariationsAffectPerformanceNandBehaviorAcroSsdifferentos.2)Nativelibrariesviajnijniiniininiinniinindrododerplatefform.3)

Java代碼在不同平台上運行時會有性能差異。 1)JVM的實現和優化策略不同,如OracleJDK和OpenJDK。 2)操作系統的特性,如內存管理和線程調度,也會影響性能。 3)可以通過選擇合適的JVM、調整JVM參數和代碼優化來提升性能。

Java'splatFormentenceHaslimitations不包括PerformanceOverhead,versionCompatibilityIsissues,挑戰WithnativelibraryIntegration,Platform-SpecificFeatures,andjvminstallation/jvminstallation/jvmintenance/jeartenance.therefactorscomplicatorscomplicatethe“ writeOnce”

PlatformIndependendecealLowsProgramStormonanyPlograwsStormanyPlatFormWithOutModification,而LileCross-PlatFormDevelopmentRequiredquiresMomePlatform-specificAdjustments.platFormIndependence,EneblesuniveByjava,EnablesuniversUniversAleversalexecutionbutmayCotutionButMayComproMisePerformance.cross.cross.cross-platformd

JITcompilationinJavaenhancesperformancewhilemaintainingplatformindependence.1)Itdynamicallytranslatesbytecodeintonativemachinecodeatruntime,optimizingfrequentlyusedcode.2)TheJVMremainsplatform-independent,allowingthesameJavaapplicationtorunondifferen

javaispopularforcross-platformdesktopapplicationsduetoits“ writeonce,runany where”哲學。 1)itusesbytiesebyTecodeThatrunsonAnyJvm-備用Platform.2)librarieslikeslikeslikeswingingandjavafxhelpcreatenative-lookingenative-lookinguisis.3)

在Java中編寫平台特定代碼的原因包括訪問特定操作系統功能、與特定硬件交互和優化性能。 1)使用JNA或JNI訪問Windows註冊表;2)通過JNI與Linux特定硬件驅動程序交互;3)通過JNI使用Metal優化macOS上的遊戲性能。儘管如此,編寫平台特定代碼會影響代碼的可移植性、增加複雜性、可能帶來性能開銷和安全風險。

Java將通過雲原生應用、多平台部署和跨語言互操作進一步提昇平台獨立性。 1)雲原生應用將使用GraalVM和Quarkus提升啟動速度。 2)Java將擴展到嵌入式設備、移動設備和量子計算機。 3)通過GraalVM,Java將與Python、JavaScript等語言無縫集成,增強跨語言互操作性。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具