首页 > 其他分享 >特殊文件

特殊文件

时间:2023-09-09 11:57:04浏览次数:30  
标签:文件 特殊 System prop println public append out

title: 特殊文件
index_img: https://tuchuangs.com/imgs/2023/08/13/c648af4d798edc8f.png
tags:
  - Java SE
categories:
  - Java SE
excerpt: 特殊文件

文本文件

普通文本文件txt:

  • 内容随便写,无格式,无限制。
  • 但是你按照规则来写也是可以读的【properties的方法来读即可】

特殊文本文件(易读、易解析):

  • properties:

    • 是集合,但不会当集合使用,往往用于配置文件
    • 键值对格式,键不可重复。
    • 适用于对象属性较少
  • XML

    • 可以表示更加复杂的关系
    • 可以自定义标签
    • 标签中可以使用属性
    • 参考html

image-20230813204126616

properties

读取数据

  • 文本文件可使用字节或字符流
构造器/方法 说明
public Properties() //构造器 用于构建Properties集合对象(空参)
public void load(Inputstream is) 通过字节输入流,读取属性文件里的键值对数据
public void load(Reader reader) 通过字符输入流,读取属性文件里的键值对数据
public string getProperty(string key) 根据键获取值(其实就是get方法的效果),独有一般不用Map提供的
public Set stringPropertyNames() 获取全部键的集合(其实就是ketSet方法的效果)

image-20230813205455157

public class Main {
    public static void main(String[] args) {
        try {
            extracted();
        } catch (FileNotFoundException e) {
            System.out.println("文件路径不存在");
        } catch (IOException e) {
            System.out.println("读取失败");
        }
    }

    private static void extracted() throws IOException {
        Properties prop = new Properties(); //properties对象

        FileReader fileReader = new FileReader("src/main/java/bid/simpleword/pl/test.properties"); //需要一个输入流

        prop.load(fileReader); //读取键值对
        fileReader.close();

        Set<String> user = prop.stringPropertyNames(); //键的集合
        user.forEach(s -> System.out.println(s + ":" + prop.getProperty(s))); //打印键值对
    }
}

image-20230813205634207

写入数据

image-20230813210027678

public class Main {
    public static void main(String[] args) {
        try {
            extracted();
        } catch (FileNotFoundException e) {
            System.out.println("文件路径不存在");
        } catch (IOException e) {
            System.out.println("读取失败");
        }
    }

    private static void extracted() throws IOException {
        Properties prop = new Properties(); //properties对象

        prop.setProperty("张1", "1232");
        prop.setProperty("张2", "1232");
        prop.setProperty("张3", "1232");
        prop.setProperty("张4", "1232");

        //输出流并追加
        FileWriter fileWriter = new FileWriter("src/main/java/bid/simpleword/pl/test.properties", true);
        prop.store(fileWriter, "存储了一些用户");
        fileWriter.close();
    }
}

image-20230813211200888

XML

<?xml version="1.0" encoding="UTF-8" ?>
<!--上方这一行必须要,且必须在最上面-->

<!--根标签[users]只能有一个-->
<users>
    <admin>
        <name>管理员</name>
    </admin>
    <user>
        <name>张三</name>
        <age>16</age>
        <sex>女</sex>
    </user>

    <user>
        <name>里斯</name>
        <age>16</age>
        <sex>男</sex>
    </user>

    <user>
        <name>张开</name>
        <age>12</age>
        <sex>女</sex>
    </user>
</users>

解析

解析使用Dom4j第三方框架,它的解析思想,类似于js的文档树

image-20230813213307885

image-20230813213943047

public class Main {
    public static void main(String[] args) throws DocumentException {
        SAXReader saxReader = new SAXReader();
        Document doc = saxReader.read("src/main/java/bid/simpleword/pl/test.xml");

        System.out.println("------根标签对象--------");
        Element rootElement = doc.getRootElement();
        System.out.println("Root Element: " + rootElement.getName());

        System.out.println("------下一级标签对象----------");
        List<Element> elements = rootElement.elements();
        for (Element element : elements) {
            System.out.println("Element Name: " + element.getName());
        }

        System.out.println("------下一级标签对象中的所有user对象----------");
        List<Element> userElements = rootElement.elements("user");
        for (Element userElement : userElements) {
            System.out.println("User Element: " + userElement.getName());
            System.out.println("Name: " + userElement.elementText("name"));
            System.out.println("Age: " + userElement.elementText("age"));
            System.out.println("Sex: " + userElement.elementText("sex"));
            System.out.println();
        }

        System.out.println("------属性值----------");
        Element admin = rootElement.element("admin");
        System.out.println("Admin Name Attribute: " + admin.attributeValue("name"));
    }
}

image-20230813220619209

生成

Stringbuilder拼接为XML,IO流写入

public class Main {
    public static void main(String[] args) throws IOException {
        StringBuilder sbr = new StringBuilder();
        sbr.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>")
                .append("\n").append("<books>")
                .append("\n").append("<book>")
                .append("\n").append("<name>入门到精通</name>")
                .append("\n").append("<version>1.0.1</version>")
                .append("\n").append("</book>")
                .append("\n").append("<book>")
                .append("\n").append("<name>入门到精通2</name>")
                .append("\n").append("<version>1.0.1</version>")
                .append("\n").append("</book>")
                .append("\n").append("</books>");
        BufferedWriter bw = new BufferedWriter(new FileWriter("src/main/java/bid/simpleword/pl/1.xml"));
        bw.write(sbr.toString());
        bw.close();   // 记得关闭流
    }
}

image-20230813221948910

标签:文件,特殊,System,prop,println,public,append,out
From: https://www.cnblogs.com/SimpleWord/p/17689193.html

相关文章

  • Python学习 -- 文件内容操作技术详解
    文件操作在编程中是一个常见的任务,Python提供了丰富而灵活的文件操作工具,能够帮助您轻松地读取、写入和处理文件内容。在本篇博客中,我们将深入探讨Python中文件操作的各种技术和方法,包括打开文件、读写文件、移动文件指针、关闭文件等。打开文件在Python中,使用内置的open函......
  • 用来练习sql的库表创建sql文件
    1.创建work.sql文件,填写以下内容--MySQLdump10.13Distrib5.7.43,forLinux(x86_64)----Host:localhostDatabase:work----------------------------------------------------------Serverversion 5.7.43/*!40101SET@OLD_CHARACTER_SET_CLIENT=@@CHARACT......
  • C-条件编译、头文件
    一、条件编译根据条件(表达式的值或者特定的宏的值)决定让代码是否参与最终的编译查看预处理的结果:gcc-Ecode.c 把预处理的结果显示到终端gcc-Ecode.c-ocode.i把预处理的结果存储到.i预处理文件1、常见的条件编译指令指令#if如果条件为真,则执行相应的操作......
  • C-文件
    一、文件的分类:文本文件:是人能看得懂的文件,存储的是字符符号的ASCII码的二进制'2''5''5'二进制文件:存储的是数据的补码二进制255  11111111二、文件IO:fopenFILE*fopen(constchar*path,constchar*mode);功能:打开或创建文件path:文件的路径......
  • 配置文件加载模块-单例配置类设计
    classConfig{有我们单例模式所需要的一些信息,首先向外提供一个统一的访问接口,然后在类内实例化一个访问对象//使用的是懒汉模式,涉及到线程安全问题,用的时候才去进行加载,所以要加锁private:staticstd::mutex_mutex;//互斥锁,来保护对象实例化的一个过程Config*_instance;//......
  • springMVC的xml文件配置
    <?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/sc......
  • py文件转换成exe文件在windows上允运行 有没有什么好方法?
    大家好,我是皮皮。一、前言前几天在Python最强王者群【哎呦喂 是豆子~】问了一个Python打包的问题,一起来看看吧。py文件转换成exe文件在windows上允运行有没有什么好方法?window上没有python。二、实现过程这里【瑜亮老师】给了一个思路和指导,如下:把用到的库你复制过去,开始......
  • # yyds干货盘点 # py文件转换成exe文件在windows上允运行 有没有什么好方法?
    大家好,我是皮皮。一、前言前几天在Python最强王者群【哎呦喂 是豆子~】问了一个Python打包的问题,一起来看看吧。py文件转换成exe文件在windows上允运行有没有什么好方法?window上没有python。二、实现过程这里【瑜亮老师】给了一个思路和指导,如下:把用到的库你复制过去,开始打包。【......
  • uniapp项目实践总结(十三)封装文件操作方法
    导语:在日常APP开发过程中,经常要进行文件的保存、读取列表以及查看和删除文件等操作,接下来就看一下具体的方法。目录原理分析方法实现实战演练案例展示原理分析主要是以下API。uni.saveFile:保存文件到本地缓存列表;uni.getSavedFileList:获取保存文件列表;uni.getSa......
  • 如何取消VSCODE文件夹折叠
    1.问题如图所示,文件夹折叠在一起,导致我无法在父文件夹中新建一个文件夹,而是只能在子文件夹中新建文件夹2.解决原因:文件夹以紧凑方式呈现,取消即可1.打开设置,在里面搜索Explorer:CompactFolders2.取消勾选即可......