特殊文件
Properties文件
特点
- 都只能是键值对
- 键不能重复
- 文件后缀一般是.properties结尾的
我们使用Properties
实例来对Properties文件进行操作
Propertites实例
- 是一个Map集合(键值对集合),但我们一般不会当集合使用
- 核心作用:Properties是用来代表属性文件的,通过Properties可以读写属性文件里的内容
使用Properties读取属性文件里的键值对数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
// 1、创建属性集对象,代表一个属性文件
Properties properties = new Properties();
System.out.println(properties);
// 2、加载属性文件信息到属性集对象中去
// public void load(InputStream is)
// properties.load(new FileInputStream("users.properties")); // 字节流
// public void load(Reader reader)
properties.load(new FileReader("users.properties")); // 字符流
System.out.println(properties); // 发现中文乱码,properties对中文识别不好,需要我们对编辑器去做一下配置
// 根据键取值
// System.out.println(properties.get("mysql"));
// public String getProperty(String key)
System.out.println(properties.getProperty("mysql"));
// 获取所有键名
// public Set<String> stringPropertyNames();
Set<String> keys = properties.stringPropertyNames();
for(String key:keys){
String value = properties.getProperty(key);
System.out.println(key + "==>" + value);
}
// 遍历数据
proerties.forEach((k,v) ->{
System.out.println(k + "===>" + v);
});
|
ide配置properties,使其支持中文
File -> Settings -> 输入encoding -> 选择 File Encodings -> 修改配置(Default encoding for properties files: 选择utf-8)并将下方的Transparent native-to-ascii conversion选中
使用Properties把键值对数据写入到属性文件里去
1
2
3
4
5
6
7
8
9
10
11
12
13
|
// 1、创建属性集对象
// public Properties() 用于构建Properties集合对象(空容器)
Properties properties = new Properties();
// 2、存入数据
// public Object setProperty(String key,String value) 保存键值对数据到Properties对象中去
properties.setProperty("admin","66666");
properties.setProperty("xiaozhao","wuji");
// 3、转存到文件
// public void store(OutputStream os,String comments) 把键值对数据,通过字节输出流写入到属性文件里去
// public void store(Writer w,String comments) 把键值对数据,通过字符输出流写入到属性文件里去
properties.store(new FileOutputStream("user.properties","this is properties file comment"));
|
xml文件
全称(Extensible Markup Language,可扩展标记语言)
本质是一种数据的格式,可以用来存储复杂的数据结构和数据关系
特点
- xml中的标签成对出现
- xml标签名可以自己定义,但必须要正确的嵌套
- xml中只有一个根标签
- xml中的标签可以有属性
- 如果一个文件中放置的是xml格式的数据,这个文件就是xml文件,后缀一般要写成.xml
注意:
特殊数据区:<![CDATA[里面内容随便写,不用担心字符转义问题]]
作用
本质是一种数据格式,可以存储复杂的数据机构和数据关系
应用场景
经常用来作为系统的配置文件;或者作为一种特殊的数据结构,在网络中进行传输
使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
// 使用Dom4J解析xml文件
// 1、创建一个SaxReader解析器对象
// public SAXReader();
SAXReader saxReader = new SAXReader();
// 2、把xml文件读成一个Document文档对象
// public Document read(String url)
// public Document read(InputStream is) 通过字节输入流读取XML文件
Document document = saxReader.read("contact.xml");
// 3、文档对象中包含了xml的全部数据,提供了方法获取数据
// Element getRootElement() 获得根元素对象
Element rootElement = document.getRootElement();
System.out.println(rootElement.getName()); // 获取根元素名称
// 4、提取子元素对象
List<Element> sonEles = rootElement.elements();
for (Element sonEle:sonEles){
System.out.println(sonEle.getName());
}
// 5、获取指定单个子元素对象
Element userEle = rootElement.element("user");
System.out.println(userEle.elementText("name"));
// 6、获取子元素的属性对象 id=xx
Attribute idAttr = contactEle.attribute("id");
System.out.println(idAtrr.getValue()); // 对应属性id的值
System.out.println(contactEle.attributeValue("id")); // 直接拿属性id的值
// 7、文本值
// 通过父元素拿到子元素文本值
System.out.println(contactEle.elementText("name"));
System.out.println(contactEle.elementTextTrim("name")); // 去掉空格
// 先拿到元素对象,再提取其文本值
Element emailEle = contactEle.element("email");
System.out.println(emailEle.getText());
|
使用程序把数据写入到xml文件中
把数据拼接成xml格式,然后用IO流写进去
1
2
3
4
5
6
7
8
9
|
StringBuilder sb = new StringBuilder();
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
sb.append("<user>\r\n");
sb.append("\t<name>").append("张三").append("</name>\r\n");
sb.append("</user>\r\n");
PrintStream ps = new PrintStream("user.xml");
ps.println(sb);
ps.close();
|
xml文档约束
就是限制xml文件只能按照某种格式进行书写
DTD文档
可以约束xml文件的编写,不能约束具体的数据类型
Schema文档
可以约束xml文件的编写和数据类型
日志技术
日志通常就是一个文件,里面记录的是程序运行过程中的各种信息
- 可以将系统执行的信息,方便的记录到指定的位置(控制台、文件中、数据库中)
- 可以随时以开关的形式控制日志的启停,无需侵入到源代码中去进行修改
日志接口
主要有两个Commons Logging(JCL)
与Simple Logging Facade for Java(SLF4J)
实现类
我们主要掌握Logback
框架,该框架是基于SLF4J
接口实现的。
因为对Commons Logging
接口不满意,有人就搞了SLF4J
;因为对Log4j的性能不满意,有人就搞了Logback
Logback日志框架
主要分为3个模块
logback-core
基础模块,是其他两个模块依赖的基础(必须有)
logback-classic
完整实现了slf4j api的模块(必须有)
Logback-access
与Tomcat和jetty等servlet容器集成,以提供http访问日志的功能(可选,以后再接触)
Logback日志框架入门
步骤
- 导入Logback框架到项目中去
- 将Logback框架的核心配置文件
logback.xml
直接拷贝到src目录下。(必须是src下)
- 创建Logback框架提供的Logger对象,然后用Logger对象调用其提供的方法就可以记录系统的日志信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
// 创建一个Logback框架的Logger日志对象,来记录日志
public static final Logger LOGGER = LoggerFactory.getLogger("Test.class");
try{
LOGGER.info("除法开始了");
chu(10,0);
LOGGER.info("除法结束了");
}catch(Exception e){
LOGGER.error("执行失败:"+e.getMessage());
}
public static void chu(int a,int b){
LOGGER.debug("参数a:"+a);
LOGGER.debug("参数b:"+b);
int c = a/b;
LOGGER.debug("结果c:"+c);
}
|