特殊文件

属性文件:属性文件的内容都是一些键值对信息,每一行都是键值对,后缀一般都是.properties结尾的。#注释

XML文件:注释<!--    -->,一般用于存储用户更加复杂的信息。作为系统的配置文件。

特殊文件_键值对

学习特殊文件:了解他们的特点、作用。学习使用程序读取里边的数据。学习使用程序把数据存储到这些文件中。

使用Map集合下的properties

读取

是一个Map集合(键值对集合),但是我们一般不会当成集合使用。 

Properties是用来代表属性文件的,通过Properties可以读写属性文件里的内容。

特殊文件_System_02

public class test {
    public static void main(String[] args) throws Exception {
        // 1. 创建一个Properties的对象出来(键值对集合,读取用)
        Properties properties = new Properties();
        System.out.println(properties);

        // 2. 开始加载属性文件中的键值对数据到properties对象中去
        properties.load(new FileReader("D:\java\daima\untitled\untitled1\src\com\lzk\test\user.properties"));
        System.out.println(properties);

        // 3. 根据键读取值
    	 System.out.println(properties.getProperty("admin"));
        System.out.println(properties.getProperty("password"));

        // 4. 遍历全部的键和值。
        Set<String> keys = properties.stringPropertyNames();
        for (String key : keys) {
            String value = properties.getProperty(key);
            System.out.println(key + "----->" + value);
        }

        properties.forEach((k, v) -> {
            System.out.println(k + "----->" + v);
        });
    }
}

user.properties

admin = lzk
password = 123456
hh = 1223


存储

public class PropertiesTest2 {
    public static void main(String[] args) throws Exception {
        // 1. 创建Properties对象并且,用它存储一些键值对数据
        Properties properties = new Properties();
        properties.setProperty("张无忌", "minmin");
        properties.setProperty("赵敏", "cuishan");
        properties.setProperty("李逍遥", "susu");

        // 2. 把properties对象中的键值对数据写入到属性文件中去
        try (FileWriter writer = new FileWriter(""D:\java\daima\untitled\untitled1\src\com\lzk\test\user1.properties")) {
            properties.store(writer, "i saved many users!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

修改

public class PropertiesTest3 {
    public static void main(String[] args) throws Exception {
        // 1. 加载属性文件中的键值对到属性对象中来。
        Properties properties = new Properties();
        
        // 2. 开始加载
        try (FileReader reader = new FileReader("day11-special-file-log\\src\\users.txt")) {
            properties.load(reader);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 3. 判断是否含有李方这个键
        if (properties.containsKey("李方")) {
            properties.setProperty("李方", "18");
        }

        // 4. 把properties对象的键值对数据重新写入到属性文件中去。
        try (FileWriter writer = new FileWriter("day11-special-file-log\\src\\users.txt")) {
            properties.store(writer, "success!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

XML文件

XML(Extensible Markup Language可扩展标记语言),可扩展指的是标签可以自定义。

特点:

1.<>称为标签,一般成对出现。

2.标签可以自定义,但是要正确嵌套。

3.只能有一个根标签。

4.标签可以有属性。

5.后缀写成.xml

格式:

<?xml version="1.0" encoding="UTF-8" ?>
<uesr>
    <user id = "1">
        <name>John</name>
        <age>25</age>
        <地址>西岸区</地址>
        <data> 3<4  && 5>6 </data>
        <![CDATA[3>5]]>
    </user>
    <user id = "2">
        <name>Mary</name>
        <age>30</age>
        <地址>东城区</地址>
    </user>
</uesr>

特殊文件_XML_03

作用和应用场景

本质是一种数据格式,可以存储复杂的数据结构和数据关系.

应用场景:经常用来做系统的配置文件;或者作为一种特殊的数据结构,在网络中传播。

解析XML文件

使用程序读取XML中的数据。程序员不需要自己写IO流来解析,难度太大。

使用框架:Dom4j,他的解析方法是一层一层的,先拿外边,再拿里边。

特殊文件_XML_04

特殊文件_XML_05

public class Dom4JTest1 {
    public static void main(String[] args) throws Exception {
        // 1. 创建一个Dom4j的SAXReader对象
        SAXReader saxReader = new SAXReader();

        // 2. 使用saxReader去读取指定路径的XML文件转换成一个Document对象
        Document document = saxReader.read("properties-xml-log-app\\src\\helloworld.xml");

        // 3. 从文档中获取XML文件的根元素对象
        Element root = document.getRootElement();
        
        // 打印根元素的名称
        System.out.println(root.getName());
    }
}

特殊文件_XML_06

getTextTrim 去除文本去除前后空格。

public class XmlExample {
    public static void main(String[] args) {
        try {
            // 创建SAXReader对象
            SAXReader reader = new SAXReader();
            // 读取XML文件,并获取Document对象
            Document document = reader.read("example.xml");

            // 获取根元素
            Element rootElement = document.getRootElement();

            // 示例:使用getName()方法
            System.out.println("Root element name: " + rootElement.getName());

            // 示例:使用elements()方法, 获取所有子元素
            List<Element> allChildElements = rootElement.elements();
            for (Element elem : allChildElements) {
                System.out.println("Child element name: " + elem.getName());
            }

            // 示例:使用elements(String name)方法, 获取特定子元素
            List<Element> specificChildElements = rootElement.elements("child");
            for (Element elem : specificChildElements) {
                System.out.println("Specific child element name: " + elem.getName());
            }

            // 示例:使用element(String name)方法, 获取单个子元素
            Element singleChild = rootElement.element("child");
            if (singleChild != null) {
                System.out.println("Single child element name: " + singleChild.getName());
            }

            // 示例:使用attributeValue(String name)方法, 获取属性值
            String attrValue = singleChild.attributeValue("attributeName");
            System.out.println("Attribute value: " + attrValue);

            // 示例:使用elementText(子元素名)方法, 获取子元素的文本内容
            String textValue = singleChild.elementText("subchild");
            System.out.println("Subchild text: " + textValue);

            // 示例:使用getText()方法, 获取元素的文本内容
            String elementText = singleChild.getText();
            System.out.println("Element text: " + elementText);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

写入XML文件(少用)

不建议使用Dom4j,推荐直接把程序里的数据使用append拼接成XML文件,然后使用IO流写出去。

约束XML文件的书写

就是限制XML文件只能按照某种格式进行书写。

约束文档(了解)

专门用来限制XML的书写格式。分类:DTD文档、Schema文档。

特殊文件_键值对_07

特殊文件_System_08