zoukankan      html  css  js  c++  java
  • Java读写配置文件——Properties类的简要使用笔记

    任何编程语言都有自己的读写配置文件的方法和格式,Java也不例外。

    在Java编程语言中读写资源文件最重要的类是Properties,功能大致如下:

    1. 读写Properties文件
    2. 读写XML文件
    3. 不仅可以读写上述两类文件,还可以读写其它格式文件如txt等,只要符合key=value格式即可.
     
    注意:资源文件中含有中文时的处理方法 
    1. 将中文字符通过工作转成utf8编码,可以通过Java自带的nativetoascii或Eclipse中的属性编辑器。
    2. 直接调用 new String(youChineseString.getBytes("ISO-8859-1"), "GBK");
     
    附:WEB程序中加载资源文件的方法
    Properties prop = null; 
    1. prop = Thread.currentThread().getContextClassLoader().getResourceAsStream("filename");
    2. prop = this.getClass().getClassLoader().getResourceAsStream("filename");
     
     
    Properties类继承自Hashtable,大致API如下:
     
     
     
    好了,直接用代码说话吧,这个类很容易使用
    看下Demo目录结构:
     
    先来个读取配置文件类:PropertiesReader.java
    关于Properties读取文件这里提供六种方法:《JAVA读取Properties的六种方法》,下面取最常用的一种
     
    关于路径的写法:(可以相对路径也可以是绝对路径)
    Class.getResourceAsStream(String path) 
    path 不以’/'开头时默认是从此类所在的包下取资源,以’/'开头则是从ClassPath(src文件)根下获取。
     
     1 package com.lcw.properties.test;
     2 
     3 import java.io.IOException;
     4 import java.io.InputStream;
     5 import java.util.Enumeration;
     6 import java.util.Properties;
     7 
     8 /**
     9  * properties文件读取类
    10  * 
    11  */
    12 public class PropertiesReader {
    13 
    14     public void getPropertiesReader(){
    15         Properties properties=new Properties();//获取Properties实例
    16         InputStream inStream=getClass().getResourceAsStream("config.properties");//获取配置文件输入流
    17         try {
    18             properties.load(inStream);//载入输入流
    19             Enumeration enumeration=properties.propertyNames();//取得配置文件里所有的key值
    20             while(enumeration.hasMoreElements()){
    21                 String key=(String) enumeration.nextElement();
    22                 System.out.println("配置文件里的key值:"+key+"=====>配置文件里的value值:"+properties.getProperty(key));//输出key值
    23             }
    24             
    25         } catch (IOException e) {
    26             e.printStackTrace();
    27         }
    28     }
    29 
    30 }

    再来个测试类:PropertiesTest.java

     1 package com.lcw.properties.test;
     2 
     3 public class PropertiesTest {
     4 
     5     /**
     6      * 测试类
     7      */
     8     public static void main(String[] args) {
     9         PropertiesReader propertiesReader=new PropertiesReader();
    10         propertiesReader.getPropertiesReader();
    11     }
    12 
    13 }

    这是配置文件信息:config.properties

    color=black
    animal=rabbit
    food=hamburger
    chinese=u6211u662Fu4E2Du6587

    看下运行效果:

      

    若要写入配置i文件也很简单,这里添加一个方法:

     1     //写入资源文件信息
     2     public void writeProperties(){
     3         Properties properties=new Properties();
     4         try {
     5             OutputStream outputStream=new FileOutputStream("config.properties");
     6             properties.setProperty("number", "2015");
     7             properties.setProperty("song", "手写的从前");
     8             properties.store(outputStream, "rabbit");
     9             outputStream.close();
    10         } catch (FileNotFoundException e) {
    11             e.printStackTrace();
    12         } catch (IOException e) {
    13             e.printStackTrace();
    14         }
    15     }

    生成文件:

    #rabbit
    #Wed Jan 07 17:16:56 CST 2015
    number=2015
    song=u6211u7231u4F60
  • 相关阅读:
    linux command lynx
    git bisect
    git blame (10)
    git crate&query&delete tag(九)
    git crate patch and check&apply patch(八)
    learning shell check host dependent pkg (8)
    【泛型】
    sql server 查询所有表的记录数
    【转】EXCEL中如何实现由总表自动得出分表
    MS Chart 打印
  • 原文地址:https://www.cnblogs.com/lichenwei/p/4208935.html
Copyright © 2011-2022 走看看