当前位置: 首页 > news >正文

网站建设刂搜金手指下拉贰伍最近国际新闻大事20条

网站建设刂搜金手指下拉贰伍,最近国际新闻大事20条,b2b平台定制开发,wordpress默认账号密码忘记前言 *.properties文件,是 Java 支持的一种配置文件类型,并且 Java 提供了 properties 类来读取 properties 文件中的信息。文件中以键值对 "键值"的形式,存储工程中会多次重复使用的配置信息,通过“Properties”类来读…

前言

        *.properties文件,是 Java 支持的一种配置文件类型,并且 Java 提供了 properties 类来读取 properties 文件中的信息。文件中以键值对 "键=值"的形式,存储工程中会多次重复使用的配置信息,通过“Properties”类来读取这些信息,以实现“一次编写,多处调用;需要修改配置文件时,只修改一处即可”的效果

        本文介绍【读写 properties 文件】,【追加数据】,【实现有序读写】,【解决中文乱码】问题。


正文

一、Properties 类简介

        Properties有一个特殊的作用,专门用来加载xxx.properties配置文件。Properties继承于Hashtable,表示了一个持久的属性集,可保存在流中或从流中加载。属性列表中,每个键及其对应值都是一个字符串。

  • 常用方法: 
方法名含义
public String getProperty(String key)用指定的键在此属性列表中搜索属性

public Object setProperty(String key,String 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?characterEncoding=utf8");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;}
  • 运行上述代码会产生几个问题:
  1. 顺序是无序的,或者说是hash排序的,并不是set的顺序;
  2. 每次write,properties文件的数据都会被覆盖掉,只展示最新数据;
  3. 如果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 List<Object> keyList = new ArrayList<Object>();/*** 默认构造方法*/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 List<Object> 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 Enumeration<Object> keys() {return new EnumerationAdapter<Object>(keyList);}/*** List到Enumeration的适配器*/private class EnumerationAdapter<T> implements Enumeration<T> {private int index = 0;private final List<T> list;private final boolean isEmpty;public EnumerationAdapter(List<T> 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)完以后创建(new)output = 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)完以后创建(new)output = 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();}}

http://www.khdw.cn/news/13957.html

相关文章:

  • 做pc网站会连带手机版营销手机系统安装
  • 网站建设销售话术seo网站权重
  • 服务器添加网站如何提高自己在百度的排名
  • 自学织梦做网站要多久企业如何网络推广
  • 烟台做网站推广的公司哪家好seo做的好的网站
  • 营销型网站建设市场怎么推广产品
  • 做外贸必应网站产品曝光谷歌全球营销
  • 做网站的职业牡丹江seo
  • wordpress css 使用方法如何将网站的关键词排名优化
  • 怎么查看网站使用空间班级优化大师免费下载
  • 做网站教程和维护网站北京网络推广有哪些公司
  • 做自媒体都有什么网站seo推广是什么意思
  • wordpress 无法显示图片深圳宝安seo外包
  • 网站开发Java与Python网络营销方案
  • 如何建立p2p网站网络黄页平台网址有哪些
  • wordpress小工具插件下载株洲seo优化首选
  • 网站备案和备案的区别百度热搜榜排名今日
  • 网站开发分站站长联盟
  • 企业邮箱登录入口foxmailseo发包技术教程
  • 淘宝做网站网络营销的基本内容有哪些
  • 郑州企业展厅设计公司郑州seo代理外包
  • 苏州知名网站建设设计公司排名福州网站seo公司
  • 苹果电脑可以做网站吗北京网站seo费用
  • 手机在线做ppt模板下载网站kol推广是什么意思
  • 公司做网站域名归谁成都搜索优化排名公司
  • 网站中转页怎么做百度学术论文查重免费
  • 怎么做网站赚钱深圳搜索seo优化排名
  • wordpress英语怎么读江门网站优化公司
  • 长沙 外贸网站建设优化大师会员兑换码
  • 做网站一定要正版系统吗网络推广网站推广方法