zoukankan      html  css  js  c++  java
  • 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;
     }

    }

  • 相关阅读:
    java的语法基础(二)
    MyBatis 的好处是什么?
    python中字符串的编码和解码
    Spring的简介和优点?
    相对于Statement,PreparedStatement的优点是什么?
    MyBatis 的好处是什么?
    .final finally finalize区别
    final类有什么用
    web广泛用到的技术:
    JDK,JRE,JVM三者的关系
  • 原文地址:https://www.cnblogs.com/33blog/p/2610780.html
Copyright © 2011-2022 走看看