基因数据库网站建设wordpress文章排版插件
- 作者: 五速梦信息网
- 时间: 2026年03月21日 10:49
当前位置: 首页 > news >正文
基因数据库网站建设,wordpress文章排版插件,移动平台开发技术,内容整合营销前言 *.properties文件#xff0c;是 Java 支持的一种配置文件类型#xff0c;并且 Java 提供了 properties 类来读取 properties 文件中的信息。文件中以键值对 键值的形式#xff0c;存储工程中会多次重复使用的配置信息#xff0c;通过“Properties”类来读… 前言 .properties文件是 Java 支持的一种配置文件类型并且 Java 提供了 properties 类来读取 properties 文件中的信息。文件中以键值对 键值的形式存储工程中会多次重复使用的配置信息通过“Properties”类来读取这些信息以实现“一次编写多处调用需要修改配置文件时只修改一处即可”的效果。 本文介绍【读写 properties 文件】【追加数据】【实现有序读写】【解决中文乱码】问题。 正文 一、Properties 类简介 Properties有一个特殊的作用专门用来加载xxx.properties配置文件。Properties继承于Hashtable表示了一个持久的属性集可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。 常用方法 方法名含义public String getProperty(String key)用指定的键在此属性列表中搜索属性 public Object setProperty(String keyString value) 向Properties中增加属性有则覆盖没有则新增public void load(InputStream in)从输入流中读取属性列表键和元素对public void load(Reader reader)按简单的面向行的格式从输入字符流中读取属性列表键和元素对public void store(OutputStream out, String comments)将此 Properties 表中的属性列表键和元素对写入输出流public void store(Writer writer, String comments)将此 Properties 表中的属性列表键和元素对写入输出字符 二、读写 properties 文件 由于Properties继承于Hashtable所以当新增属性写入到 .properties 文件的时候结果显示的顺序可能并不是我们当初set属性的顺序。这个问题要注意我们再后面会解决。 Properties继承于Hashtable 借助IO流利用Properties类操作.properties文件 // .properties文件的相对路径private static final String FILE_ADDRESS src/main/resources/application.properties;GetMapping(/test)public String test() {log.info(test 接口调用【开始】 ——————–);this.writeIntoText(new Properties());Properties prop this.readFromText();log.info(jdbc.username properties.getProperty(jdbc.username));log.info(test 接口调用【结束】 ——————–);return success;}/** 模拟向 properties 文件写入数据/private void writeIntoText(Properties properties){OutputStream output null;try {output new FileOutputStream(FILE_ADDRESS);properties.setProperty(jdbc.driver, com.mysql.jdbc.Driver);properties.setProperty(jdbc.url, jdbc:mysql://localhost:3306/mybatis?characterEncodingutf8);properties.setProperty(jdbc.username, root);properties.setProperty(jdbc.password, 123456);properties.store(output, tjm modify);} catch (IOException io) {io.printStackTrace();} finally {if (output ! null) {try {output.close();} catch (IOException e) {e.printStackTrace();}}}}/** 读取 properties 文件返回 Properties 对象/private Properties readFromText(){Properties properties new Properties();InputStream input null;try {input new FileInputStream(FILE_ADDRESS);properties.load(input);} catch (IOException io) {io.printStackTrace();} finally {if (input ! null) {try {input.close();} catch (IOException e) {e.printStackTrace();}}}return properties;} 运行上述代码会产生几个问题 顺序是无序的或者说是hash排序的并不是set的顺序每次writeproperties文件的数据都会被覆盖掉只展示最新数据如果value里面包含中文那一定会是乱码 下面我们就以此解决这几个问题。 三、解决追加、有序、乱码问题 2.1 实现有序读写 自定义一个PropertiesUtil类该类继承自Properties。PropertiesUtil提供一个返回由key按照存入顺序组成的List的方法getKeyList()这样就可以解决有序写的问题。至于读嘛也是一样的道理。 如何保证getKeyList()方法返回的就是有序的key组成的集合 通过查询Properties方法的源码发现Properties.load()方法从.properties文件加载内容时是按照文件顺序进行读取而Properties.setProperty()方法底层调用的是父类HashTable.put()方法进行储存。 HashTable本身就是无序的所以解决办法就是让PropertiesUtil持有一个私有的可以有序存储key的集合然后重写父类的put()方法在方法体中照常通过super.put()进行属性的存储同时将key添加到存储key的集合中。 再查源码发现 Properties将当前对象的内容存放到指定的输出流的方法又2个save()和store()但是它们的底层逻辑都是一样的都是通过调用Properties.keys()方法获取一个Enumeration然后对该Enumeration进行遍历依次将对应的key和value写入到输出流中。要保证写入是有序的就要保证遍历keys()方法返回的Enumeration取出的元素key是有序的。 所以得到最终的解决方法是重写keys()方法保证遍历Enumeration得到的key是有序的。 PropertiesUtil类的完整代码如下 import java.io.; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.Properties;/*** Description: Java Punk* Created: by JavaPunk on 2023/5/10 16:33* Version: 1.0.0/ public class PropertiesUtil extends Properties {private static final long serialVersionUID 1L;private ListObject keyList new ArrayListObject();/** 默认构造方法/public PropertiesUtil() {}/** 从指定路径加载信息到Properties* param path/public PropertiesUtil(String path) {try {InputStream is new FileInputStream(path);this.load(is);} catch (FileNotFoundException e) {e.printStackTrace();throw new RuntimeException(指定文件不存在);} catch (IOException e) {e.printStackTrace();}}/** 重写put方法按照property的存入顺序保存key到keyList遇到重复的后者将覆盖前者。/Overridepublic synchronized Object put(Object key, Object value) {this.removeKeyIfExists(key);keyList.add(key);return super.put(key, value);}/** 重写remove方法删除属性时清除keyList中对应的key。/Overridepublic synchronized Object remove(Object key) {this.removeKeyIfExists(key);return super.remove(key);}/** keyList中存在指定的key时则将其删除/private void removeKeyIfExists(Object key) {keyList.remove(key);}/** 获取Properties中key的有序集合* return/public ListObject getKeyList() {return keyList;}/** 保存Properties到指定文件默认使用UTF-8编码* param path 指定文件路径/public void store(String path) {this.store(path, UTF-8);}/** 保存Properties到指定文件并指定对应存放编码* param path 指定路径* param charset 文件编码/public void store(String path, String charset) {if (path ! null !.equals(path)) {try {OutputStream os new FileOutputStream(path);BufferedWriter bw new BufferedWriter(new OutputStreamWriter(os, charset));this.store(bw, null);bw.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}} else {throw new RuntimeException(存储路径不能为空!);}}/** 重写keys方法返回根据keyList适配的Enumeration且保持HashTable keys()方法的原有语义* 每次都调用返回一个新的Enumeration对象且和之前的不产生冲突/Overridepublic synchronized EnumerationObject keys() {return new EnumerationAdapterObject(keyList);}/** List到Enumeration的适配器/private class EnumerationAdapterT implements EnumerationT {private int index 0;private final ListT list;private final boolean isEmpty;public EnumerationAdapter(ListT list) {this.list list;this.isEmpty list.isEmpty();}public boolean hasMoreElements() {// isEmpty的引入是为了更贴近HashTable原有的语义在HashTable中添加元素前调用其keys()方法获得一个Enumeration的引用// 之后往HashTable中添加数据后调用之前获取到的Enumeration的hasMoreElements()将返回false但如果此时重新获取一个// Enumeration的引用则新Enumeration的hasMoreElements()将返回true而且之后对HashTable数据的增、删、改都是可以在// nextElement中获取到的。return !isEmpty index list.size();}public T nextElement() {if (this.hasMoreElements()) {return list.get(index);}return null;}} } 2.2 实现数据追加 在上面【读写 properties 文件】中简单介绍了写入的方法因为每次都会 new Properties()所以每次都会把源数据全部覆盖掉。清楚了原因自然也就找到了解决办法写入之前先将源文件加载出来再进行有序的追加。 源码先加载输入流随后set新属性最后store输出流 /** 模拟向 properties 文件追加写入数据/private void addToText(){// 将 PropertiesUtil 换成重写前的 Properties 类最后写入的顺序是hash排序的// Properties properties new Properties();PropertiesUtil properties new PropertiesUtil();InputStream input null;OutputStream output null;try {// 先用输入流加载.properties文件input new BufferedInputStream(new FileInputStream(FILE_ADDRESS));properties.load(new InputStreamReader(input));// 输出流FileOutputStream对象必须在Properties类加载load完以后创建newoutput new FileOutputStream(FILE_ADDRESS);properties.setProperty(jdbc2.username, PropertiesUtil Orderly test);properties.store(output, tjm modify);} catch (IOException io) {io.printStackTrace();} finally {if (output ! null) {try {output.close();} catch (IOException e) {e.printStackTrace();}}if (input ! null) {try {input.close();} catch (IOException e) {e.printStackTrace();}}}} PropertiesUtil运行结果 Properties运行结果 2.3 解决中文乱码 上面测试使用的都是中文如果参数中包含【中文】就会出现乱码问题解决办法输入输出时设置【charsetName utf-8】。 代码展示 /** 模拟向 properties 文件追加写入数据/private void addToText(){// 将 PropertiesUtil 换成重写前的 Properties 类最后写入的顺序是hash排序的 // Properties properties new Properties();PropertiesUtil properties new PropertiesUtil();InputStream input null;OutputStream output null;try {// 先用输入流加载.properties文件input new BufferedInputStream(new FileInputStream(FILE_ADDRESS));properties.load(new InputStreamReader(input, utf-8));// 输出流FileOutputStream对象必须在Properties类加载load完以后创建newoutput new FileOutputStream(FILE_ADDRESS);properties.setProperty(jdbc3.username, PropertiesUtil 有序测试);properties.store(new OutputStreamWriter(output, utf-8), tjm modify);} catch (IOException io) {io.printStackTrace();} finally {if (output ! null) {try {output.close();} catch (IOException e) {e.printStackTrace();}}if (input ! null) {try {input.close();} catch (IOException e) {e.printStackTrace();}}}}/** 读取 properties 文件返回 Properties 对象/private Properties readFromText(){PropertiesUtil properties new PropertiesUtil();InputStream input null;try {input new FileInputStream(FILE_ADDRESS);properties.load(new InputStreamReader(input, utf-8));log.info(举例说明jdbc3.username properties.getProperty(jdbc3.username));} catch (IOException io) {io.printStackTrace();} finally {if (input ! null) {try {input.close();} catch (IOException e) {e.printStackTrace();}}}return properties;} 结果展示 四、PropertiesConfiguration介绍 PropertiesConfiguration 是 Apache 帮我们实现按照文件的顺序读取properties文件的类Properties类能做的它都能做。不仅如此他还有许多方便实用的附加功能。 示例代码功能展示 /** 模拟向 properties 文件写入数据/private void setParam(){try {PropertiesConfiguration propsConfig new PropertiesConfiguration(FILE_ADDRESS);propsConfig.setEncoding(utf-8);// 修改属性之后自动保存省去了propsConfig.save()过程propsConfig.setAutoSave(true);// setProperty遇到同名key会替换value没有则新增propsConfig.setProperty(set.name, 123456);// addProperty只会新增即使遇到遇到同名key也不会替换propsConfig.addProperty(add.name, 456789);}catch (Exception ex) {ex.printStackTrace();}}/** 模拟向 properties 文件读取数据*/private void getParam(){try {PropertiesConfiguration propsConfig new PropertiesConfiguration(FILE_ADDRESS);propsConfig.setEncoding(utf-8);String username propsConfig.getString(jdbc.username);log.info(举例说明jdbc.username username);}catch (Exception ex) {ex.printStackTrace();}}
相关文章
-
基金管理公司司网站建设要求wordpress posts page
基金管理公司司网站建设要求wordpress posts page
- 技术栈
- 2026年03月21日
-
基础微网站开发咨询网站的几种提交方式
基础微网站开发咨询网站的几种提交方式
- 技术栈
- 2026年03月21日
-
基础微网站开发动态网站怎么做外部优化
基础微网站开发动态网站怎么做外部优化
- 技术栈
- 2026年03月21日
-
基于asp网站开发 论文网络维护工具
基于asp网站开发 论文网络维护工具
- 技术栈
- 2026年03月21日
-
基于ASP与Access数据库的网站开发人力资源做网站的好处
基于ASP与Access数据库的网站开发人力资源做网站的好处
- 技术栈
- 2026年03月21日
-
基于c 的网站开发百度推广要自己做网站吗
基于c 的网站开发百度推广要自己做网站吗
- 技术栈
- 2026年03月21日
