package com.LY;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Properties;
public class TestMain {
// 根据key读取value
public static String readValue(String filePath, String key) {
  Properties props = new Properties();
  try {
    InputStream in = new BufferedInputStream(new FileInputStream(filePath));
    props.load(in);
    String value = props.getProperty(key);
    System.out.println(key + value);
    return value;
  } catch (Exception e) {
    e.printStackTrace();
    return null;
  }
}
  // 读取properties的全部信息
  public static void readProperties(String filePath) {
  Properties props = new Properties();
  try {
    InputStream in = new BufferedInputStream(new FileInputStream(filePath));
    props.load(in);
    Enumeration en = props.propertyNames();
    while (en.hasMoreElements()) {
      String key = (String) en.nextElement();
      String Property = props.getProperty(key);
      System.out.println(key + Property);
    }
  } catch (Exception e) { 
    e.printStackTrace();
  }
}
// 写入properties信息
public static void writeProperties(String filePath, String parameterName,
  String parameterValue) {
  Properties prop = new Properties();
    try {
      InputStream fis = new FileInputStream(filePath);
      // 从输入流中读取属性列表(键和元素对)
      prop.load(fis);
      // 调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
      // 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
      OutputStream fos = new FileOutputStream(filePath);
      prop.setProperty(parameterName, parameterValue);
      // 以适合使用 load 方法加载到 Properties表中的格式,
      // 将此 Properties 表中的属性列表(键和元素对)写入输出流
      prop.store(fos, "Update '" + parameterName+ "' value");
    } catch (IOException e) {
      System.err.println("Visit " + filePath + " for updating " + parameterName + " value error");
    }
  }
  public static void main(String[] args) {
    readValue("info.properties", "url");
    writeProperties("info.properties", "age","22");
    readProperties("info.properties");
    System.out.println("OK");
  }
}

用java读写properties文件的代码的更多相关文章

  1. Java读写.properties文件实例,解决中文乱码问题

    package com.lxk.propertyFileTest; import java.io.*; import java.util.Properties; /** * 读写properties文 ...

  2. java读写Properties文件

                Java   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ...

  3. Java读写资源文件类Properties

    Java中读写资源文件最重要的类是Properties 1) 资源文件要求如下: 1.properties文件是一个文本文件 2.properties文件的语法有两种,一种是注释,一种属性配置.  注 ...

  4. 使用JAVA读写Properties属性文件

     使用JAVA读写Properties属性文件 Properties属性文件在JAVA应用程序中是经常可以看得见的,也是特别重要的一类文件.它用来配置应用程序的一些信息,不过这些信息一般都是比较少的数 ...

  5. 【转】Java 读写Properties配置文件

    [转]Java 读写Properties配置文件 1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形 ...

  6. Java 读写Properties配置文件

    Java 读写Properties配置文件 JAVA操作properties文件 1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了M ...

  7. java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)

     java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...

  8. 用java读取properties文件--转

    今天为了通过java读取properties文件,google了很长时间,终于找到了.现在特记录之和大家一起分享.     下面直接贴出代码:java类 public class Mytest pub ...

  9. java 读取properties文件总结

    一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...

随机推荐

  1. REPLACE...IN.....WITH.... 的使用

    REPLACE...IN.....WITH....   的使用,例子用于改变alv的gt_fieldcat_alv LOOP AT gt_fieldcat_alv ASSIGNING <fs_f ...

  2. Java 多线程 (并发)总结

    一.概念 1. 维基百科解释 进程是什么? http://zh.wikipedia.org/wiki/%E8%BF%9B%E7%A8%8B 线程是什么? http://zh.wikipedia.org ...

  3. Xamarin 后台持续定位与提示

    IOS后台持续运行对于c#程序员不懂得ios后台机制的是存在一定困扰的.特别是ios9过后对后台和安全进行了更严格的限制 好了废话不多说 一 设置info.plist权限信息 参考: 后台模式:htt ...

  4. ubuntu安装iscsi

    ubuntu安装iscsi target端:apt-get install iscsitarget ubuntu安装iscsi initiator端:apt-get install open-iscs ...

  5. mysql 主从同步出问题,重新修复从库 - web架构研究

    mysql 主从同步出问题,重新修复从库 - web架构研究     mysql 主从同步出问题,重新修复从库    0     昨天由于操作失误,在从库上执行一堆sql之后,导致主从同步错误,并且已 ...

  6. Android---OpenGL ES之添加动作

    本文译自:http://developer.android.com/training/graphics/opengl/motion.html 在屏幕上绘制对象是OpenGL的最基本功能,你可以使用其他 ...

  7. 摘要算法CRC8、CRC16、CRC32,MD2 、MD4、MD5,SHA1、SHA256、SHA384、SHA512,RIPEMD、PANAMA、TIGER、ADLER32

    1.CRC8.CRC16.CRC32 CRC(Cyclic Redundancy Check,循环冗余校验)算法出现时间较长,应用也十分广泛,尤其是通讯领域,现在应用最多的就是 CRC32 算法,它产 ...

  8. 明晚8点,捷微团队QQ群公开课,解说jeewx2.0版本号maven环境的搭建入门!

    2014-08-13号晚8点,捷微团队QQ群公开课,解说jeewx2.0版本号maven环境的搭建入门! 讲师:刘强(团队成员) QQ群:287090836 (JAVA版本号微信开源项目) http: ...

  9. Cocos2d-x 3.1.1 学习日志8--2分钟让你知道cocos2d-x3.1.1 文本类别

    实际上文本经常使用的三个,LabelTTF,LabelBMF和LabelAtlas.而他们使用非常相似.所以,你会只举一反三,非常快就能够掌握了. <span style="font- ...

  10. poj 1094

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26911   Accepted: 92 ...