搜索
首页Javajava教程Java如何处理properties资源文件

Java如何处理properties资源文件

Apr 28, 2023 pm 02:19 PM
javaproperties

在Java语言中,使用一种以.properties为扩展名的文本文件作为资源文件,该类型的文件的内容格式为类似:

#注释语句
some_key=some_value

形式。以#开头的行作为注释行,ResourceBundle类处理时会加以忽略;其余的行可以以 key名=value值 的形式加以记述。

Java的ResourceBundle类可以对这种形式的文件加以处理。

ResourceBundle类的使用方法也非常简单。我们使用一个例子来说明。

我们假设有下面2个properties文件:

TestProperties.properties   view plainprint?  #key=value     userIdLabel=User Id:      userNameLabel=User Name:     #key=value userIdLabel=User Id:   userNameLabel=User Name:   TestProperties_zh_CN.properties   view plainprint?  #key=value     userIdLabel=用户ID:      userNameLabel=用户名:     #key=value userIdLabel=用户ID:   userNameLabel=用户名:

大家可能注意到TestProperties_zh_CN.properties文件名中有一个_zh_CN名称,该名称其实是用于资源文件的本地化处理。什么是本地化呢?我们简单说明一下:我们在进行系统开发时,很多时候需要为不同地区的用户准备不同的界面,比如,如果一个系统同时面向 英语圈的用户以及面向中国的用户,我们就必须为系统准备2套界面(包括消息),一套为英语界面,一套为中文界面。当然,除了界面不同之外,系统的处理过程完全一样。当然我们不可能为它们分别开发2套不同的系统,怎么办呢?这就需要用到资源的本地化处理。也就是说,根据用户所处的地区或语言的不同,分别准备不同的资源文件,这样就可以为不同的用户准备不同的界面但使用的却是同一套系统逻辑。

我们上面的2个文件就是2套不同的资源。

我们是使用ResourceBundle类处理不同资源的代码:

TestProperties.java   view plainprint?  package com.test.properties;          import java.util.Enumeration;     import java.util.Locale;     import java.util.ResourceBundle;          public class TestProperties  {              public static void main(String []args) {     String resourceFile = "com.test.properties.TestProperties";     //创建一个默认的ResourceBundle对象     //ResourceBundle会查找包com.test.properties下的TestProperties.properties的文件     //com.test.properties是资源的包名,它跟普通java类的命名规则完全一样:     //- 区分大小写     //- 扩展名 .properties 省略。就像对于类可以省略掉 .class扩展名一样     //- 资源文件必须位于指定包的路径之下(位于所指定的classpath中)     //另外,对于非西欧字符(比如中日韩文等),需要使用native2ascii命令或类似工具将其转换成ascii码文件格式,否则会显示乱码。     System.out.println("---Default Locale---");     ResourceBundle resource = ResourceBundle.getBundle(resourceFile);          testResourceBundle(resource);          System.out.println("---Locale.SIMPLIFIED_CHINESE---");          //创建一个指定Locale(本地化)的ResourceBundle对象,这里指定为Locale.SIMPLIFIED_CHINESE     //所以ResourceBundle会查找com.test.properties.TestProperties_zh_CN.properties的文件     //     //中文相关的Locale有:     //Locale.SIMPLIFIED_CHINESE : zh_CN     resource = ResourceBundle.getBundle(resourceFile, Locale.SIMPLIFIED_CHINESE);     //Locale.CHINA  : zh_CN     //Locale.CHINESE: zh     testResourceBundle(resource);          //显示     //         }                  private static void testResourceBundle(ResourceBundle resource) {     //取得指定关键字的value值     String userIdLabel = resource.getString("userIdLabel");     System.out.println(userIdLabel);          //取得所有key值     Enumeration enu = resource.getKeys();          System.out.println("keys:");     while(enu.hasMoreElements()) {         System.out.println(enu.nextElement());     }         }     }     package com.test.properties;   import java.util.Enumeration;  import java.util.Locale;  import java.util.ResourceBundle;   public class TestProperties  {       public static void main(String []args) {  String resourceFile = "com.test.properties.TestProperties";  //创建一个默认的ResourceBundle对象  //ResourceBundle会查找包com.test.properties下的TestProperties.properties的文件  //com.test.properties是资源的包名,它跟普通java类的命名规则完全一样:  //- 区分大小写  //- 扩展名 .properties 省略。就像对于类可以省略掉 .class扩展名一样  //- 资源文件必须位于指定包的路径之下(位于所指定的classpath中)  //另外,对于非西欧字符(比如中日韩文等),需要使用native2ascii命令或类似工具将其转换成ascii码文件格式,否则会显示乱码。  System.out.println("---Default Locale---");  ResourceBundle resource = ResourceBundle.getBundle(resourceFile);   testResourceBundle(resource);   System.out.println("---Locale.SIMPLIFIED_CHINESE---");   //创建一个指定Locale(本地化)的ResourceBundle对象,这里指定为Locale.SIMPLIFIED_CHINESE  //所以ResourceBundle会查找com.test.properties.TestProperties_zh_CN.properties的文件  //  //中文相关的Locale有:  //Locale.SIMPLIFIED_CHINESE : zh_CN  resource = ResourceBundle.getBundle(resourceFile, Locale.SIMPLIFIED_CHINESE);  //Locale.CHINA  : zh_CN  //Locale.CHINESE: zh  testResourceBundle(resource);   //显示  //      }            private static void testResourceBundle(ResourceBundle resource) {  //取得指定关键字的value值  String userIdLabel = resource.getString("userIdLabel");  System.out.println(userIdLabel);   //取得所有key值  Enumeration enu = resource.getKeys();   System.out.println("keys:");  while(enu.hasMoreElements()) {      System.out.println(enu.nextElement());  }      }  }

解说:

1,为了便于理解,我们把解说放在Java源代码中了,这里不再详述了。

2,对于中文资源文件TestProperties_zh_CN.properties,需要使用native2ascii 命令将其转换为ascii码。例如:

native2ascii -encoding UTF-8 c:\TestProperties_zh_CN.properties c:\java\com\test\properties\TestProperties_zh_CN.properties

至于native2ascii的详细用法这里不做详述了。

3,将上面3个文件都保存在 c:\java\com\test\properties\ 目录下。其中TestProperties_zh_CN.properties为经过native2ascii转换后的文件。

4,编译执行,将会在屏幕上显示:

c:\java\javac com.test.properties.TestProperties.java

c:\java\java com.test.properties.TestProperties
---Default Locale---
User Id:
keys:
userNameLabel
userIdLabel
---Locale.SIMPLIFIED_CHINESE---
用户ID:
keys:
userNameLabel
userIdLabel

以上是Java如何处理properties资源文件的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:亿速云。如有侵权,请联系admin@php.cn删除
JVM性能与其他语言JVM性能与其他语言May 14, 2025 am 12:16 AM

JVM'SperformanceIsCompetitiveWithOtherRuntimes,operingabalanceOfspeed,安全性和生产性。1)JVMUSESJITCOMPILATIONFORDYNAMICOPTIMIZAIZATIONS.2)c提供NativePernativePerformanceButlanceButlactsjvm'ssafetyFeatures.3)

Java平台独立性:使用示例Java平台独立性:使用示例May 14, 2025 am 12:14 AM

JavaachievesPlatFormIndependencEthroughTheJavavIrtualMachine(JVM),允许CodeTorunonAnyPlatFormWithAjvm.1)codeisscompiledIntobytecode,notmachine-specificodificcode.2)bytecodeisisteredbytheybytheybytheybythejvm,enablingcross-platerssectectectectectross-eenablingcrossectectectectectection.2)

JVM架构:深入研究Java虚拟机JVM架构:深入研究Java虚拟机May 14, 2025 am 12:12 AM

TheJVMisanabstractcomputingmachinecrucialforrunningJavaprogramsduetoitsplatform-independentarchitecture.Itincludes:1)ClassLoaderforloadingclasses,2)RuntimeDataAreafordatastorage,3)ExecutionEnginewithInterpreter,JITCompiler,andGarbageCollectorforbytec

JVM:JVM与操作系统有关吗?JVM:JVM与操作系统有关吗?May 14, 2025 am 12:11 AM

JVMhasacloserelationshipwiththeOSasittranslatesJavabytecodeintomachine-specificinstructions,managesmemory,andhandlesgarbagecollection.ThisrelationshipallowsJavatorunonvariousOSenvironments,butitalsopresentschallengeslikedifferentJVMbehaviorsandOS-spe

Java:写一次,在任何地方跑步(WORA) - 深入了解平台独立性Java:写一次,在任何地方跑步(WORA) - 深入了解平台独立性May 14, 2025 am 12:05 AM

Java实现“一次编写,到处运行”通过编译成字节码并在Java虚拟机(JVM)上运行。1)编写Java代码并编译成字节码。2)字节码在任何安装了JVM的平台上运行。3)使用Java原生接口(JNI)处理平台特定功能。尽管存在挑战,如JVM一致性和平台特定库的使用,但WORA大大提高了开发效率和部署灵活性。

Java平台独立性:与不同的操作系统的兼容性Java平台独立性:与不同的操作系统的兼容性May 13, 2025 am 12:11 AM

JavaachievesPlatFormIndependencethroughTheJavavIrtualMachine(JVM),允许Codetorunondifferentoperatingsystemsswithoutmodification.thejvmcompilesjavacodeintoplatform-interploplatform-interpectentbybyteentbytybyteentbybytecode,whatittheninternterninterpretsandectectececutesoneonthepecificos,atrafficteyos,Afferctinginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginging

什么功能使Java仍然强大什么功能使Java仍然强大May 13, 2025 am 12:05 AM

JavaispoperfulduetoitsplatFormitiondence,对象与偏见,RichstandardLibrary,PerformanceCapabilities和StrongsecurityFeatures.1)Platform-dimplighandependectionceallowsenceallowsenceallowsenceallowsencationSapplicationStornanyDevicesupportingJava.2)

顶级Java功能:开发人员的综合指南顶级Java功能:开发人员的综合指南May 13, 2025 am 12:04 AM

Java的顶级功能包括:1)面向对象编程,支持多态性,提升代码的灵活性和可维护性;2)异常处理机制,通过try-catch-finally块提高代码的鲁棒性;3)垃圾回收,简化内存管理;4)泛型,增强类型安全性;5)ambda表达式和函数式编程,使代码更简洁和表达性强;6)丰富的标准库,提供优化过的数据结构和算法。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用