首页 > 编程知识 正文

java如何使用缓存,java读取jar包下的配置文件

时间:2023-05-06 15:41:15 阅读:173927 作者:2059

一.通过jdk提供的java.util.Properties类

由于该类继承自java.util.HashTable并实现了Map接口,因此可以使用相应的方法处理属性文件,但不推荐使用put和putAll这两种方法。 因为put方法不仅可以存储在String型的value中,还可以存储在Object型的value中。 因此,java.util.Properties类提供了用于处理属性文件的getProperty ()和setProperty () )方法。 此外,store或save ) (已过期)保存属性值(将属性值写入. properties配置文件)。 在使用之前,必须加载属性文件。 有两种方法: load和loadFromXML。

load提供了两种方法的重载: load (输入流)、load (读),通过这两种方法可以加载属性文件。

InputStream可以通过以下各种方式获得:

1 .从当前类加载器的getResourceAsStream方法中获取下载地址

inputstream instream=test properties.class.getclass loader ().getresourceasstream (' test.properties ' );

2 .从文件获取

inputstream instream=new file inputstream (new file (' file path );

3 .也通过类加载器获取,与第一个相同

inputstream in=class loader.getsystemresourceasstream ('文件路径' );

4 .在servlet中,还可以从context中检索InputStream

inputstream in=context.getresourceasstream (' file path );

5 .通过URL获取

URLURL=newurl('path );

inputstream instream=URL.open stream (;

读取方法如下。

Propertiesprop=newProperties (;

PROP.load(Instream );

string key=prop.getproperty (' username );

//stringkey=(string ) prop.get(username );

在java.util.ResourceBundle类中读取

这个方式比使用Properties方便。

ResourceBundle.getBundle ) )从静态方法中获取

资源绑定是抽象类。 通过这种方式获取properties属性文件不需要使用. properties扩展名,而只需要文件名。

resourcebundleresource=resource bundle.get bundle (' com/mmq/test ' ); //test是属性文件名,放在程序包com.mmq下,放在src下时,直接使用test即可

string key=resource.getstring (username );

从InputStream读取

获取InputStream的方法与上述相同。 省略说明。

资源资源=newpropertyresourcebundleresource (instream );

注:使用过程中遇到的最大问题可能是配置文件的路径问题。 如果配置文件位于当前类的包下,则必须用包名称限定它。 例如,如果test.properties位于com.mmq包下,则为com/mmq/test.properties (如果属性文件位于src根目录下,则直接为test.properties )

三.实例

ResourceLoader.java

1 packagecom.bijian.study; 2

3 importjava.io.File; 4 importjava.io.FileInputStream; 5 importjava.util.HashMap; 6 importjava.util.Map; 7 importjava.util.Properties; 8

9公共类资源加载器{ 10 }

11专用sourceloaderloader=newresourceloader (; 12 privatestaticmaploadermap=new hashmap (; 13

14权限资源加载器{ 15 } 16

17公共资源加载器(18 return loader; (二十)二十

21 publicpropertiesgetpropfromproperties (string filename ) throws exception

23属性prop=loader map.get (文件名称; 24if (专业!=null}{25returnprop; 26 ) 27字符串文件路径=null; 28 stringconfigpath=system.getproperty (configure path ); 29

30if(configpath==null ) { 31文件路径=this.getclass ().getClassLoader ).get resource (filename ).getPath ) ) 32 ) else { 33文件路径=config path '/'文件名称; 35prop=newproperties (); 36prop.load(newfileInputstream ) new file (file path ); 37

38loadermap.put(filename,prop ); 39返回提示; 40 }41 }

PropertiesUtil.java

1 packagecom.bijian.study; 2

3 importjava.util.Properties; 4 import Java.util.concurrent.concurrent hashmap; 5 import Java.util.concurrent.concurrent map; 6

7 /**

在ConcurrentMap中,单击属性文件中的key-value9 */

10 publicclasspropertiesutil { 11 }

12 privatestaticresourceloaderloader=resource loader.getinstance (; 13 privatestaticconcurrentmapconfigmap=newconcurrenthashmap (; 14 privatestaticfinalstringdefault _ config _ file=' test.properties '; 15

16 privatestaticpropertiesprop=null; 17

18 publicstaticstringgetstringbykey (string key,String propName ) 19try ) 20 prop=loader.getpropfromproperties ) propname 21 23 ) 24key=key.Trim (; 25 if (! configmap.containskey(key ) (26if ) prop.getproperty (key )!=null(27configmap.put ) key,prop.getproperty ) key ); 28 } 29 } 30 returnconfigmap.get (key ); 31632

33公共字符串密钥(string key ) 34返回字符串密钥(key,DEFAULT_CONFIG_FILE ); 6636

37 publicstaticpropertiesgetproperties () 38try ) 39 return loader.getpropfromproperties ) default_config_file; 40 ) catch(exceptione ) {41 e.printStackTrace ); 42返回空; 43 }44 }45 }

Constant.java

1 packagecom.bijian.study; 2

3公共类常量{1}4{2}

5 publicstaticfinalstringtest=properties util.getstringbykey (' test ',' test.properties ' ); 6 }

Main.java

1 packagecom.bijian.study; 2

3公共类主{1} {2}4{3}

5 publicstaticvoidmain (string [ ] args ) {6}

7 system.out.println (constant.test; 8 }9 }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。