java中使用Properties读取和写入XMl文件

上代码: 

import java.io.*;
import java.nio.file.Paths;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class XMLUtil {

    //写入xml文件
    public static void storeTOXML(Properties properties,OutputStream outputStream,String comment) throws IOException {
        storeTOXML(properties,outputStream,comment,"UTF-8");
    }

    public static void storeTOXML(Properties properties,OutputStream outputStream,String comment,String encoding) throws IOException {
        properties.storeToXML(outputStream,comment,encoding);
    }

    public static void storeTOXML(Properties properties, File file, String comment) throws IOException {
        storeTOXML(properties,file,comment,"UTF-8");
    }

    public static void storeTOXML(Properties properties,File file,String comment,String encoding) throws IOException {
        properties.storeToXML(new FileOutputStream(file),comment,encoding);
    }

    //读取xml文件
    public static void loadFromXML(Properties properties,File file) throws IOException {
        loadFromXML(properties,new FileInputStream(file));
    }

    public static void loadFromXML(Properties properties,InputStream inputStream) throws IOException {
        properties.loadFromXML(inputStream);
    }


    //写入文件
//    public static void main(String[] args) {
//        File file = Paths.get("aa","bb.xml").toFile();
//        if (!file.getParentFile().exists())
//            file.getParentFile().mkdirs();
//        Properties properties = new Properties();
//        properties.setProperty("nn","南宁");
//        properties.setProperty("bh","北海");
//
//        try {
//            storeTOXML(properties,file,"我是注释");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
    //读取文件
    public static void main(String[] args) {
        File file = Paths.get("aa","bb.xml").toFile();
        Properties properties = new Properties();
        try {
            loadFromXML(properties,file);
            properties.list(System.out);//控制台打印所以属性

            //根据key获取属性
            System.out.println("-- 根据key获取属性 --");
            System.out.println(properties.getProperty("nn"));
            System.out.println(properties.getProperty("bh"));

            //获取所有键值
            System.out.println("-- 获取所有键值(推荐使用Iterator方式读取集合) --");
            Set<Map.Entry<Object, Object>> set = properties.entrySet();
            Iterator<Map.Entry<Object, Object>> iterator = set.iterator();
            while (iterator.hasNext()){
                Map.Entry<Object,Object> me = iterator.next();
                System.out.println(me.getKey()+"-->"+me.getValue());
            }

            System.out.println("-- 获取所有键值2(foreach方式读取) --");
            Set<Map.Entry<Object, Object>> set2 = properties.entrySet();
            for (Map.Entry<Object,Object> me:set2) {
                System.out.println(me.getKey()+"-->"+me.getValue());
            }

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

文件内容:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Properties 文件和 XML 文件都是常用的配置文件格式,用于存储应用程序的配置信息。它们之间的主要区别如下: 1. 格式不同 Properties 文件使用简单的 key-value 格式,每一行表示一个属性。例如: ``` database.driver=com.mysql.cj.jdbc.Driver database.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC database.username=root database.password=123456 ``` XML 文件采用标签和属性的方式来表示配置信息。例如: ``` <database> <driver>com.mysql.cj.jdbc.Driver</driver> <url>jdbc:mysql://localhost:3306/test?serverTimezone=UTC</url> <username>root</username> <password>123456</password> </database> ``` 2. 扩展性不同 XML 文件比 Properties 文件更加灵活和可扩展,可以定义更加复杂的数据结构。XML 文件支持嵌套、属性、命名空间等特性,适用于更加复杂的配置场景。而 Properties 文件只能支持简单的 key-value 格式,不支持嵌套和复杂的数据结构。 3. 可读性不同 XML 文件比 Properties 文件更加易读,可以定义更加有意义的标签和属性名。而 Properties 文件只能使用简单的键值对,不太容易理解和维护。 4. 读取方式不同 Java 可以使用 Properties 类来读取写入 Properties 文件,也可以使用 DOM 或 SAX 解析器来读取写入 XML 文件。 总之,Properties 文件适用于简单的配置场景,而 XML 文件适用于更加复杂的配置场景。在实际开发,应根据具体的需求选择适合的配置文件格式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值