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. Android四大组件--Broadcast Receiver具体解释

    本文主要讲述了: 一.BroadcastReceiver概述: 二.BroadcastReceiver事件分类 三.BroadcastReceiver事件的编程流程 四.两类BroadcastRece ...

  2. 使用visual c++ 2005编译64位可执行文件

    最近需要将一个32位的程序移植到64位上,由于原来是使用vs2003写的,vs2003本身并不支持编译64位系统上,只能升级到vs2005以上版本.个人还是比较喜欢vs2005,对c++来说,vs20 ...

  3. C#检查foreach为null判断

    1.foreach遍历列表或数组时,如果list或数组为null,就会报错,如下图: 2.不知道微软封装foreach的为什么不先检查要遍历的对象是否为null,这样就导致,我们在写代码时,遍历列表时 ...

  4. 怎样使用CMenu类

    CMenu类从CObject类派生而来.为什么要使用CMenu类呢?AppWzard不是把菜单做好了吗?在资源编辑器上修改菜单不是很方便吗?     我是个vc++初学者,自从当斑竹以来,天天看贴子, ...

  5. LV在系统重启后不能自动激活(boot.lvm&after.loca)

    同类相关文章:http://blog.csdn.net/laven54/article/details/9121661 最近发现suse11sp2的系统解决了异常死机的问题之后,又引入了另外的问题,比 ...

  6. java.lang.ClassCastException: sun.proxy.$Proxy11 cannot be cast to分析

    报这个错,只有一个原因,就是你转化的类型不对. 如果你的类是一个单实体类,也就是没有继承或是接口别的类. public class HjmServiceImpl {} 那么这样写就可以: HjmSer ...

  7. ThinkPhp学习03

    原文:ThinkPhp学习03 一.ThinkPHP 3 的输出      (重点) a.通过 echo 等PHP原生的输出方式在页面中输出 b.通过display方法输出   想分配变量可以使用as ...

  8. ThinkPhp学习01

    原文:ThinkPhp学习01 一.ThinkPHP的介绍           MVC  M - Model 模型                工作:负责数据的操作  V - View  视图(模板 ...

  9. CURD特性

    本节课大纲: 一.ThinkPHP 3 的CURD介绍 (了解) 二.ThinkPHP 3 读取数据 (重点) 对数据的读取 Read $m=new Model('User'); ##返回一个实例 $ ...

  10. jTDS驱动兼容性问题

    Java连接SQL Server 2000数据库时,有两种方法: (1)通过Microsoft的JDBC驱动连接.此JDBC驱动共有三个文件,分别是mssqlserver.jar.msutil.jar ...