找别人做的网站问什么域名解析后还是上线不,九一制作网站,wordpress 文件管理插件,东莞网站视频一、前言在项目的开发过程中#xff0c;为了统一配置的管理#xff0c;我们经常需要将一些配置信息根据环境的不同#xff0c;配置在不同的properties中#xff0c;然后从里面进行读取。而Properties类作为最基础也是最经常使用的类#xff0c;通过本文我们来学习一下它的…一、前言在项目的开发过程中为了统一配置的管理我们经常需要将一些配置信息根据环境的不同配置在不同的properties中然后从里面进行读取。而Properties类作为最基础也是最经常使用的类通过本文我们来学习一下它的使用然后再顺便学习下其他几种读取properties文件的方式。二、Properties和ResourceBundle类Properties表示一个持久的属性集属性列表通过key-value的形式存在并且key和value都是字符串。我们先来看一下它的继承结构1. Properties 继承结构publicclass Properties extends Hashtable {/*** A property list that contains default values for any keys not* found in this property list.** serial*/protected Properties defaults;public Properties() {this(null);}public Properties(Properties defaults) {this.defaults defaults;}}看到它的继承结构就知道这个类已经存在好久了。该类继承自Hashtable所以该类拥有Map的一切功能所以Map的put或者putAll方法都可以使用。不过由于Properties中的每个key及value都是字符串所以官方强烈反对使用它们因为这些方法允许key或者value不是字符串如果在set或get操作的时候不是字符串的话则会提示异常所以建议使用的则是诸如setProperty这些方法。2. Properties常用方法这里Map相关的方法就不介绍了主要介绍下自定义的方法2.1 setProperty方法public synchronized Object setProperty(String key, String value) {return put(key, value);}底层通过Hashtable的put方法来实现该方法的目的就是强制对属性的key和value都使用字符串的形式2.2 getProperties方法public String getProperty(String key)public String getProperty(String key, String defaultValue) {String val getProperty(key);return (val null) ? defaultValue : val;}获取属性列表中属性的key对应的值第二个重载方法表示如果获取不到值返回参数中提供的默认值。2.3 load方法public synchronized void load(Reader reader) throws IOExceptionpublic synchronized void load(InputStream inStream) throws IOExceptionload方法表示从输入流(字节流和字符流)中读取属性列表到Properties中读取的时候按照行进行读取。而有关读取行及相关转义的说明可以参考对应的API文档上面有详细的说明。2.4 loadFromXML方法public synchronized void loadFromXML(InputStream in)throws IOException, InvalidPropertiesFormatException从输入流中读取XML文件中的所有属性注意XML文档必须有相应的DTD声明2.5 store方法public void store(Writer writer, String comments)throws IOExceptionpublic void store(OutputStream out, String comments)throws IOException和load的功能相反将Properties的属性列表写入到输出流其中参数表示对属性列表的说明。2.6 storeToXML方法public void storeToXML(OutputStream os, String comment)throws IOExceptionpublic void storeToXML(OutputStream os, String comment, String encoding)throws IOException将属性写入到xml并可以指定的编码格式。2.7 propertyNames和stringPropertyNames方法public Enumeration propertyNames()public Set stringPropertyNames()两个方法都是返回Properties属性列表中所有key前者返回所有枚举后者返回类型是字符串注意如果没有在主属性列表中找到同名的键则在默认属性列表中进行查找。2.8 list方法public void list(PrintStream out)public void list(PrintWriter out)将属性列表输出到指定的输出流这个方法对调试很有用。3. ResourceBundle简介ResourceBundle没有继承什么类是一个单个的抽象类该类可以说是国际化版的Properties简单说就是可以根据本地化或语言的不同读取不同的配置文件但要注意的一点是使用ResourceBundle读取的时候properties的命名是有一定规范的名称_语言代码_国家代码.properties// 如果是默认的自定义名.properties// 例如myres_en_US.propertiesmyres_zh_CN.propertiesmyres.properties当指定的Locale是CN的时候如果myres_zh_CN.properties、myres.properties两个文件都存在则优先会使用myres_zh_CN.properties当myres_zh_CN.properties不存在时候会使用默认的myres.properties。4. ResourceBundle常用方法4.1 getBundle方法ResourceBundle提供了多个重载的静态getBundle方法用于获取资源文件这里我们不多介绍后续看实例即可public static final ResourceBundle getBundle(String baseName)public static final ResourceBundle getBundle(String baseName,Locale locale)public static ResourceBundle getBundle(String baseName, Locale locale,ClassLoader loader)public static final ResourceBundle getBundle(String baseName,Control control)public static ResourceBundle getBundle(String baseName, Locale targetLocale,ClassLoader loader, Control control)4.2 getObjectgetStringgetStringArray方法getString方法比较简单就是根据key获取属性列表中key对应的valuekey和value都是StringgetStringArray方法用于获取字符串数组返回值是字符串数组getObject方法通用的获取方法获取其他任何类型public final String getString(String key)public final String[] getStringArray(String key)public final Object getObject(String key)4.3 keySetgetKeyscontainsKey方法这几个方法都比较简单见名知义其中getKeys表示返回key的枚举形式。public Set keySet()public abstract Enumeration getKeys();public boolean containsKey(String key)4.4 getBaseBundleNamegetLocale方法getBaseBundleName方法就是获取加载的资源文件的文件名的getLocale获取本地化环境信息的。public String getBaseBundleName()public Locale getLocale()4.5 clearCache方法通过getBundle方法读取资源文件获取ResourceBundle时是从缓存中获取的如果已经缓存工厂方法将多次返回相同的资源实例而clearCache方法就是用于清除缓存的public static final void clearCache()public static final void clearCache(ClassLoader loader)4. 简单示例接下来我们来简单看下这些方法的相关使用说明。4.1 Properties 通过store方法写入对应的文件中首先我们调用setProperty方法会将key-value存于内存中此时可以通过getProperty方法读取propertyNames方法进行遍历但是并没有将键值对持久化到属性文件中故需要调用store方法持久化键值对到属性文件中。Properties properties new Properties();try {OutputStream output new FileOutputStream(cache.properties);properties.setProperty(redis.server.address, 127.0.0.1);properties.setProperty(redis.server.port, 6379);properties.setProperty(redis.server.password, );properties.setProperty(redis.server.timeout, 2000);properties.store(output, 缓存文件配置测试);} catch (IOException io) {io.printStackTrace();} finally {...}最终生成的cache.properties文件#缓存文件配置测试#Sun Aug 19 12:27:05 CST 2018redis.server.timeout2000redis.server.address127.0.0.1redis.server.passwordredis.server.port63794.2 Properties使用load方法加载同样我们可以通过load方法将属性文件中的属性加载到Properties对象中然后进行访问Properties properties new Properties();InputStream inputStream null;try {inputStream new FileInputStream(cache.properties);properties.load(inputStream);for (String key : properties.stringPropertyNames()) {System.out.println(key properties.getProperty(key));}} catch (IOException io) {io.printStackTrace();} finally {...}最终结果redis.server.timeout2000redis.server.address127.0.0.1redis.server.passwordredis.server.port6379因为Properties其实是一个Map所以Map的遍历方式也适用于Properties也可也借助Properties的propertyNames方法来进行遍历Enumeration e properties.propertyNames();while (e.hasMoreElements()) {String key (String) e.nextElement();String value properties.getProperty(key);System.out.println(key value);}4.3 ResourceBundle简单实例我们先定义三个资源文件放到src的根目录下面myres.propertiesaaagoodbbbthanksmyres_en_US.propertiesaaagoodbbbthanksmyres_zh_CN.propertiesaaa好bbb谢谢添加测试代码public static void main(String[] args) {Locale locale1 new Locale(zh, CN);ResourceBundle resb1 ResourceBundle.getBundle(cache, locale1);System.out.println(resb1.getString(aaa));ResourceBundle resb2 ResourceBundle.getBundle(cache, Locale.getDefault());System.out.println(resb1.getString(aaa));Locale locale3 new Locale(en, US);ResourceBundle resb3 ResourceBundle.getBundle(cache, locale3);System.out.println(resb3.getString(aaa));}output好好good这里需要注意下通过getBundle方法来获取的时候参数不用加properties后缀只需要文件名就可以了并且默认访问的路径是src如果文件保存在其他目录记得修改到对应的目录。5. 其他读取properties文件的方法5.1 Properties其他获取InputStream的方法在这里其实读取properties都是通过Properties来实现的不过不同的是获取InputStream流的方式我们来看一下各种获取InputStream流的方式从File文件获取InputStream inputStream new FileInputStream(new File (cache.properties));根据ClassLoader的getResourceAsStream方法来获取。其中该方法又分为两种方式Class.getResourceAsStream(String path) path 不以’/开头时默认是从此类所在的包下取资源以’/开头则是从ClassPath根下获取。其实只是通过path构造一个绝对路径最终还是由ClassLoader获取资源Class.getClassLoader.getResourceAsStream(String path) 默认则是从ClassPath根下获取path不能以’/开头最终是由ClassLoader获取资源ServletContext.getResourceAsStream(String path)默认从WebAPP根目录下取资源Tomcat下path是否以’/开头无所谓当然这和具体的容器实现有关InputStream inputStream PropertiesTest.class.getResourceAsStream(cache.properties);通过URL来获取URL url new URL(path);InputStream inputStream url.openStream();如果是Spring环境还可以通过ResourceLoader的getResource得到Resource然后通过Resource的getInputStream来得到流ResourceLoader resourceLoader new DefaultResourceLoader();Resource resource resourceLoader.getResource(/config/cache.properties);inputStream resource.getInputStream();5.2 ResourceBundle类相关方法前面也已经简单介绍过我们可以借助java.util.ResourceBundle的getBundle静态方法来获取资源实例Locale locale1 new Locale(zh, CN);ResourceBundle resb1 ResourceBundle.getBundle(cache, locale1);另外也可以借助实现类 PropertyResourceBundle 通过从输入流中进行读取inputStream new FileInputStream(cache.properties);ResourceBundle resource new PropertyResourceBundle(inputStream);不过如果有兴趣的话可以看下PropertyResourceBundle的构造方法它其实是借助Properties和HashMap来实现的public PropertyResourceBundle (InputStream stream) throws IOException {Properties properties new Properties();properties.load(stream);lookup new HashMap(properties);}private Map lookup;三、总结到这里就基本介绍完了Properties和ResourceBundle类及如何读取properties文件其实介绍的都比较基础需要注意的可能就两点吧一是路径的问题二是流的关闭和异常处理问题。