首页 > 编程语言 >解析properties文件通用Java工具类

解析properties文件通用Java工具类

时间:2024-03-06 22:03:04浏览次数:26  
标签:Java String prop key catch new 解析 properties toSaveMap

import com.sun.xml.internal.bind.v2.TODO;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;


public class PropertiesUtil {

    /**
     * 方法: getProperties <br>
     * <p>
     * 描述: 根据key获取value <br>
     * <p>
     *
     * @param key
     * @return
     */
    public static String getProperties(String key) {
        Properties prop = new Properties();
        InputStream url = PropertiesUtil.class.getResourceAsStream("/conf/deploy.properties");
        FileInputStream in = null;
        try {
            prop.load(url);
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                    in = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


        return (String) prop.get(key);

    }

	/**
	 * 根据文件路径&key获取value
	 * @param path
	 * @param key
	 * @return
	 */
    public static String getProperties(String path, String key) {
        Properties prop = new Properties();
        InputStream url = null;
        FileInputStream in = null;
        try {
            url = new FileInputStream(path);
        } catch (FileNotFoundException e2) {
// TODO Auto-generated catch block
            e2.printStackTrace();
        }

        try {
            prop.load(url);
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                    in = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return (String) prop.get(key);

    }

	/**
	 * 添加key&value
	 * @param key
	 * @param value
	 */
    public static void addProperties(String key, String value) {

        InputStream url = PropertiesUtil.class.getResourceAsStream("/conf/deploy.properties");
        Properties prop = new Properties();
        try {
            prop.load(url);
        } catch (IOException e) {
            e.printStackTrace();
        }

		/*Map toSaveMap = new HashMap();
		Set keys = prop.keySet();
		for (Iterator itr = keys.iterator(); itr.hasNext();) {
		String oldKey = (String) itr.next();
		Object oldValue = prop.get(oldKey);
		toSaveMap.put(oldKey, oldValue);
		}
		toSaveMap.put(key,value);
		
		prop.putAll(toSaveMap);
		
		System.out.println(toSaveMap.toString());*/
        prop.put(key, value);


        try {
            OutputStream outputStream = new FileOutputStream(new File(PropertiesUtil.class.getResource("/conf/deploy.properties").getFile()));
            prop.store(outputStream, null);
            outputStream.flush();
            outputStream.close();
            System.out.println("添加成功");
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

	/**
	 * 设置key & value 
	 * @param path
	 * @param key
	 * @param value
	 */
	public static void addProperties(String path, String key, String value) {
        InputStream url = null;
        try {
            url = new FileInputStream(path);
        } catch (FileNotFoundException e1) {
		// TODO Auto-generated catch block
            e1.printStackTrace();
        }
        Properties prop = new Properties();
        try {
            prop.load(url);
        } catch (IOException e) {
            e.printStackTrace();
        }


        Map toSaveMap = new HashMap();
        Set keys = prop.keySet();
        for (Iterator itr = keys.iterator(); itr.hasNext(); ) {
            String oldKey = (String) itr.next();
            Object oldValue = prop.get(oldKey);
            toSaveMap.put(oldKey, oldValue);
        }
        toSaveMap.put(key, value);

        prop.putAll(toSaveMap);

        System.out.println(toSaveMap.toString());
		
        try {
            OutputStream outputStream = new FileOutputStream(new File(path));
            prop.store(outputStream, null);
            outputStream.flush();
            outputStream.close();
            System.out.println("添加成功");
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

	/**
	 * 修改
	 * @param key
	 * @param value
	 */
	public static void updateProperties(String key, String value) {
        InputStream url = PropertiesUtil.class.getResourceAsStream("/conf/deploy.properties");
        Properties prop = new Properties();
        try {
            prop.load(url);
        } catch (IOException e) {
            e.printStackTrace();
        }


		/*Map toSaveMap = new HashMap();
		Set keys = prop.keySet();
		for (Iterator itr = keys.iterator(); itr.hasNext();) {
		String oldKey = (String) itr.next();
		Object oldValue = prop.get(oldKey);
		toSaveMap.put(oldKey, oldValue);
		}
		toSaveMap.put(key,value);
		
		prop.putAll(toSaveMap);
		
		System.out.println(toSaveMap.toString());*/
        prop.put(key, value);


        try {
            OutputStream outputStream = new FileOutputStream(new File(PropertiesUtil.class.getResource("/conf/deploy.properties").getFile()));
            prop.store(outputStream, null);
            outputStream.flush();
            outputStream.close();
            System.out.println("修改成功");
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

	/**
	 * 修改
	 * @param path
	 * @param key
	 * @param value
	 */
    public static void updateProperties(String path, String key, String value) {
        InputStream url = null;
        try {
            url = new FileInputStream(path);
        } catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
            e1.printStackTrace();
        }
        Properties prop = new Properties();
        try {
            prop.load(url);
        } catch (IOException e) {
            e.printStackTrace();
        }

		/*Map toSaveMap = new HashMap();
		Set keys = prop.keySet();
		for (Iterator itr = keys.iterator(); itr.hasNext();) {
		String oldKey = (String) itr.next();
		Object oldValue = prop.get(oldKey);
		toSaveMap.put(oldKey, oldValue);
		}
		toSaveMap.put(key,value);
		
		prop.putAll(toSaveMap);
		
		System.out.println(toSaveMap.toString());*/
        prop.put(key, value);


        try {
            OutputStream outputStream = new FileOutputStream(new File(path));
            prop.store(outputStream, null);
            outputStream.flush();
            outputStream.close();
            System.out.println("修改成功");
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

	/**
	 * 删除 
	 * @param key
	 */
	public void delProperties(String key) {
		//将文件中key,value先拿出来
        InputStream url = PropertiesUtil.class.getResourceAsStream("/conf/deploy.properties");
        Properties prop = new Properties();
        try {
            prop.load(url);
        } catch (IOException e) {
            e.printStackTrace();
        }

		/*Map toSaveMap = new HashMap();
		Set keys = prop.keySet();
		for (Iterator itr = keys.iterator(); itr.hasNext();)
		{
		String oldKey = (String) itr.next();
		Object oldValue = prop.get(oldKey);
		toSaveMap.put(oldKey, oldValue);
		}
		prop.clear();
		toSaveMap.remove(key);
		
		prop.putAll(toSaveMap);*/
        prop.remove(key);

		//将所有键值对再写入文件
        try {

            FileOutputStream outputStream = new FileOutputStream(PropertiesUtil.class.getResource("/conf/deploy.properties").getFile());

            prop.store(outputStream, null);
            outputStream.flush();
            outputStream.close();

            System.out.println("删除成功");
        } catch (IOException e) {

            e.printStackTrace();

        }

    }

	/**
	 * 删除
	 * @param path
	 * @param key
	 */
    public void delProperties(String path, String key) {

		//将文件中key,value先拿出来
        InputStream url = null;
        try {
            url = new FileInputStream(path);
        } catch (FileNotFoundException e1) {
		// TODO Auto-generated catch block
            e1.printStackTrace();
        }
        Properties prop = new Properties();
        try {
            prop.load(url);
        } catch (IOException e) {
            e.printStackTrace();
        }

		/*Map toSaveMap = new HashMap();
		Set keys = prop.keySet();
		for (Iterator itr = keys.iterator(); itr.hasNext();)
		{
		String oldKey = (String) itr.next();
		Object oldValue = prop.get(oldKey);
		toSaveMap.put(oldKey, oldValue);
		}
		prop.clear();
		toSaveMap.remove(key);
		
		prop.putAll(toSaveMap);
		
		System.out.println(toSaveMap.toString());*/
        prop.remove(key);

		//将所有键值对再写入文件
        try {

            FileOutputStream outputStream = new FileOutputStream(path);

            prop.store(outputStream, null);
            outputStream.flush();
            outputStream.close();

            System.out.println("删除成功");
        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}

标签:Java,String,prop,key,catch,new,解析,properties,toSaveMap
From: https://www.cnblogs.com/lisong0626/p/18057699

相关文章

  • Java 枚举(Enums)解析:提高代码可读性与易维护性
    接口在Java中,实现抽象的另一种方式是使用接口。接口定义接口是一个完全抽象的类,用于将具有空方法体的相关方法分组://接口interfaceAnimal{publicvoidanimalSound();//接口方法(没有具体实现体)publicvoidrun();//接口方法(没有具体实现体)}实现接口要访问......
  • 卡码java基础课 | 9.打印正方形
    学习内容:通过一道题目来学习使用循环嵌套。例题:解:点击查看代码importjava.util.Scanner;publicclassMain{publicstaticvoidmain(String[]args){Scannersc=newScanner(System.in);intn=sc.nextInt();for(intro......
  • java定时任务
    一单机定时任务Timerjava.util.Timer是JDK1.3开始就已经支持的一种定时任务的实现方式。Timer内部使用一个叫做TaskQueue的类存放定时任务,它是一个基于最小堆实现的优先级队列。TaskQueue会按照任务距离下一次执行时间的大小将任务排序,保证在堆顶的任务最先执行。这样......
  • 卡码java基础课 | 8.奇怪的信
    学习内容:通过一道题加深对取模和除法的理解例题:解:点击查看代码importjava.util.Scanner;publicclassMain{publicstaticvoidmain(String[]args){Scannersc=newScanner(System.in);while(sc.hasNext()){intnum......
  • 从零开始搭建Springboot开发环境(Java8+Git+Maven+MySQL+Idea)之一步到位
    说明所谓万事开头难,对于初学Java和Springboot框架的小伙伴往往会花不少时间在开发环境搭建上面。究其原因其实还是不熟悉,作为在IT界摸爬滚打数年的老司机,对于各种开发环境搭建已经了然于胸,自己当年也是这么过来的。今天我就毕其功于一役,解放大家的时间,让凡人的环境配置见鬼去吧......
  • JAVA API:ArrayList(泛型类)基本使用
    ArrayList代表的是一种集合,一种容器,类似于数组。 容器主要操作:增删改查   packagecom.itheima.ArrayList;importjava.util.ArrayList;importjava.util.List;publicclassdemo{publicstaticvoidmain(String[]args){ArrayListlist=new......
  • 卡码java基础课 | 7.摆平积木
    学习内容:用一道题目来练习ArrayList的遍历和访问操作。例题:解:点击查看代码importjava.util.ArrayList;importjava.util.Scanner;publicclassMain{publicstaticvoidmain(String[]args){Scannersc=newScanner(System.in);in......
  • Java学习总结 Day2
    Java学习总结Day2构造器publicclassperson{//一个类默认会有一个方法(构造器)Stringname;intage;//实例化初始值/*1.使用new必须有构造器,本质是调用构造器*2.初始化值*3.快捷键alt+insert*/publicperson(){}//有......
  • JAVA API:String String使用注意事项
    包:分门别类程序的工具,类似文件夹  JAVAlang包下不需要导包的。   String:     String常用方法:          String注意事项:              ......
  • java复习笔记 - 1
           java是一门面向对象的语言,其解决问题的方式是通过封装属性和方法为对象,通过调用对象的不同方法来达到解决问题的步骤。其本身一开始封装了不少类,可以直接使用,常见的比如String,包装类,集合类,文件类,异常类等常用的,还有一些关于数字处理的后面再说。因为最近在看数据......