石家庄网站建设石家庄,北京seo的排名优化,小学校园网站建设简介,网页游戏制作培训文章目录 前言一、properties文件1.1properties格式介绍1.2读取项目resource/templates下面properties并处理中文乱码问题1.3读取本地properties并处理中文乱码问题1.4修改properties文件 二、XML文件2.1xml文件格式2.2读取xml文件2.3写xml文件 前言
在开发当中我们经常需要用… 文章目录 前言一、properties文件1.1properties格式介绍1.2读取项目resource/templates下面properties并处理中文乱码问题1.3读取本地properties并处理中文乱码问题1.4修改properties文件 二、XML文件2.1xml文件格式2.2读取xml文件2.3写xml文件 前言
在开发当中我们经常需要用到配置文件我们可以在配置文件中很方便的设置需要进行修改和配置的参数值有时候我们本地开发和生产环境中有些参数也不一样比如数据库IP和账号密码等、要读取的文件夹路径、日志备份的时间和周期等等有些人可能会说直接将这些值定义为final变量不久可以了吗这样的缺点是我们将项目部署到生产环境中时还要重新调整Java代码然后重新生成jar包或者war包而定义到配置文件中可以直接打开jar进行修改我们在配置文件中设置这些参数就很方便后续进行修改。 在java中常用的配置文件有properties、xml和txt本文就来介绍一下这三种配置文件的读写方法。 一、properties文件
1.1properties格式介绍
properties文件是一个文本文件以“键值”的方式书写一个属性的配置信息。 从上面的类结构图可以看出它继承了Hashtable并实现了Map接口 properties的属性配置键值前后的空格在解析时候会被忽略。 注 释前面加上#号
1.2读取项目resource/templates下面properties并处理中文乱码问题
读取properties文件需要注意的是如果读取出的值含有中文的话会出现乱码的问题需要再进行处理。 该properties文件放在项目resource/templates下面 代码如下 //读取properties资源文件public static String getProperties() {Properties properties new Properties();try {InputStream resourceAsStream SysSetParaEditController.class.getClassLoader().getResourceAsStream(getGKCX.properties);properties.load(resourceAsStream);String head properties.getProperty(head);String content properties.getProperty(content);String body properties.getProperty(body);String function properties.getProperty(function);String arg properties.getProperty(arg);String functionend properties.getProperty(functionend);String bodyend properties.getProperty(bodyend);String contentend properties.getProperty(contentend);System.out.println(head -- head);System.out.println(content -- content);System.out.println(body -- body);System.out.println(function -- function);System.out.println(arg -- arg);System.out.println(functionend -- functionend);System.out.println(bodyend -- bodyend);System.out.println(contentend -- contentend);resourceAsStream.close(); //关闭流}catch (IOException e) {e.printStackTrace();}//处理中文乱码String imgFolder properties.getProperty(imgFolder);try {imgFolder new String(imgFolder.getBytes(ISO-8859-1), GBK); // 处理中文乱码}catch (UnsupportedEncodingException e) {e.printStackTrace();}System.out.println(imgFolder);return imgFolder;}1.3读取本地properties并处理中文乱码问题
如果properties文件在本地的话可以这样进行读取 代码如下 //读取properties资源文件public static String getProperties() {Properties pro new Properties();int maxTotal 0;int maxIdel 0;String host null;int port 0;try {pro.load(new FileReader(D:\\sun\\getGKCX.properties));String imgFolder properties.getProperty(imgFolder);imgFolder new String(imgFolder.getBytes(ISO-8859-1), GBK); // 处理中文乱码} catch (IOException e) {e.printStackTrace();}System.out.println(imgFolder);return imgFolder;}1.4修改properties文件 public static void writePropertiesFile(String filename){Properties properties new Properties();try{//方法一使用FileWriter properties.setProperty(imgFolder, D:\\sun);//创建字节输出流/字符输出流构造方法中绑定要输出的目的地FileWriter fwnew FileWriter(D://a.txt);//使用Properties集合中的方法store把集合中的临时数据持久写入到硬盘中properties.store(fw, save data);//释放资源fw.close();//方法二使用OutputStream OutputStream outputStream new FileOutputStream(filename);properties.setProperty(imgFolder, D:\\sun\\img);properties.store(outputStream, author: sun);outputStream.close();}catch (IOException e){e.printStackTrace();}}二、XML文件
2.1xml文件格式
ML 指可扩展标记语言EXtensible Markup Language是一种标记语言很类似 HTML。 在XML中你可以拓展发明自己的标签XML没有预定义的标签XML允许创作者定义或设计自己的标签和文档结构 基本结构 XML文档的后缀名 .xml XML的第一行必须定义文档声明 在项目中经常见到的xml文件那就是pom.xml相信大家对这个一定都不陌生吧 还有mybatis使用的mybatis.xml
?xml version1.0 encodingUTF-8 ?
!DOCTYPE configuration PUBLIC -//mybatis.org//DTD Config 3.0//EN http://mybatis.org/dtd/mybatis-3-config.dtd
configuration!--environments defaultdevelopmentenvironment iddevelopmenttransactionManager typeJDBC/dataSource typePOOLEDproperty namedriver valuecom.mysql.jdbc.Driver/property nameurl valuejdbc:mysql://127.0.0.1/table?useUnicodetrueamp;characterEncodingutf-8amp;allowMultiQueriestrueamp;useSSLfalseamp;severTimezoneGMT%2B8/property nameusername valueroot/property namepassword value123456//dataSource/environment/environments--mappersmapper resourcecom/sun/server/mapper/xml/TScdLogMapper.xml/mapper resourcecom/sun/server/mapper/xml/TScdOperateMapper.xml/mapper resourcecom/sun/server/mapper/xml/TScdOperatePartsMapper.xml/mapper resourcecom/sun/server/mapper/xml/TSysParainfoMapper.xml//mappers
/configuration2.2读取xml文件
代码如下示例
public static void readPropertiesFileFromXML(String filename){Properties properties new Properties();try{InputStream inputStream new FileInputStream(filename);properties.loadFromXML(inputStream);inputStream.close();}catch (IOException e){e.printStackTrace();}String imgFolder properties.getProperty(imgFolder);; //XML中的中文不用处理乱码正常显示}2.3写xml文件
代码如下示例
//写资源文件到XML文件含中文 public static void writePropertiesFileToXML(String filename){Properties properties new Properties();try{OutputStream outputStream new FileOutputStream(filename);properties.setProperty(imgFolder, D:\\sun\\img);properties.storeToXML(outputStream, author: sun);outputStream.close();}catch (IOException e){e.printStackTrace();}}