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();
            }
    
        }
    }

  • 相关阅读:
    mysql 5.7.3.0-m13安装教程
    angular的$http.post()提交数据到Java后台接收不到参数值问题的解决方法
    samentic 在IE9 不支持 transition 的解决方案
    Html
    html
    ng-style 的坑
    js 和 jq 控制 checkbox
    highchart访问一次后台服务返回多张图表数据
    highchart 动态刷新(可用于制作股票时时走势)
    c# 实现 java 的 System.currentTimeMillis() 值
  • 原文地址:https://www.cnblogs.com/xiamaojjie/p/15041515.html
Copyright © 2011-2022 走看看