zoukankan      html  css  js  c++  java
  • JAVA,读写properties文件

    代码如下:properties工具类

    package com.java.app01;
    
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Properties;
    import java.util.Set;
    
    public class PropertiesUtil {
        public static void main(String[] args) {
            Properties properties = readProperties("src/main/resources/account.properties");
            String pwd2 = properties.getProperty("pwd2");
            System.out.println(pwd2);
    
            HashMap<String, String> map = new HashMap<>();
            map.put("user1", "pwd1");
            map.put("user2", "pwd2");
            writeProperties(map,"src/main/resources/account1.properties");
    
        }
    
        // 读取properties文件
        public static Properties readProperties(String readFilePath) {
            Properties properties = new Properties();
            File file = new File(readFilePath);
            if (!file.exists()) {
                System.out.println("输入文件路径不存在");
                return null;
            }
            FileReader reader = null;
            try {
                reader = new FileReader(readFilePath);
                properties.load(reader);
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                return properties;
            }
    
        }
        // 写入properties文件,参数传文件路径和map数据
        public static void writeProperties(HashMap<String, String> mapData,String writeFilePath) {
            Properties properties = new Properties();
            File file = new File(writeFilePath);
            if (!file.exists()) {
                System.out.println("输入文件路径不存在");
                return;
            }
            try {
                Set<String> set = mapData.keySet();
                for (String key : set) {
                    String value = mapData.get(key);
                    properties.setProperty(key, value);
                }
                FileWriter writer = new FileWriter(writeFilePath);
                properties.store(writer, "data");
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    }

  • 相关阅读:
    spring-boot 中application.properties的各种配置
    spring-boot 集合mybatis 的分页查询
    spring boot热部署pom.xml配置
    ssm框架整合
    mybatis逆向工程
    整合hibernate的lucene大数据模糊查询
    QBC查询、离线条件查询(DetachedCriteric)和分页查询模版
    虚拟机vmnet0、vmnet1和vmnet8的区别
    centOS7-配置网络地址
    在windows中使用Navicat连接Linux虚拟机中的mysql数据库
  • 原文地址:https://www.cnblogs.com/xiamaojjie/p/15041515.html
Copyright © 2011-2022 走看看