java 操作Properties文件

package cn.studio.util;

import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;

import java.util.Properties;

public class Properties_Utils {

 public static Properties props;
 public static InputStream in;
 public static OutputStream out;

 static {
  try {
   URL url = Properties_Utils.class.getClassLoader().getResource(
     "time.properties");

   File file;
   file = new File(url.toURI());

   out = new FileOutputStream(file);
   props = new Properties();
   in = Properties_Utils.class.getResourceAsStream("/user.properties");
   props.load(in);
  } catch (FileNotFoundException e1) {
   e1.printStackTrace();
  } catch (URISyntaxException e1) {
   e1.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 // 根据key读取value
 public static String readValue(String key) {
  String value = props.getProperty(key);
  return value;

 }

 // 根据key增加值value
 public static String writeValue(String key, String value) {
  props.setProperty(key, value);
  return value;
 }

 public static void addProperty(String key, String value) {

  props.setProperty(key, value);

  try {
   props.store(out, null);
  } catch (IOException e) {
   e.printStackTrace();
  }

 }

 // 查找某个值是否在key中
 public static boolean isInKey(String key, String value) {
  System.out.println(readValue(key));
  String[] values = readValue(key).split(",");
  for (String v : values) {
   if (v.equals(value)) {
    return true;
   }
  }
  return false;
 }

}

原文地址:https://www.cnblogs.com/33blog/p/2610780.html