posts - 70,comments - 408,trackbacks - 0

发个例子大家自己看哈.
package control;

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","21");
        readProperties("info.properties" );
        System.out.println("OK");
    }
}

posted on 2006-08-21 15:35 我心依旧 阅读(62443) 评论(15)  编辑  收藏

FeedBack:
# re: 用JAVA轻松操作properties文件
2008-05-09 17:02 | anlyhz@163.com
有点收获,嘿嘿。。。  回复  更多评论
  
# re: 用JAVA轻松操作properties文件
2008-05-31 11:07 | SMF
不错,这是我需要的,谢谢  回复  更多评论
  
# re: 用JAVA轻松操作properties文件
2008-09-02 14:46 | ghost
3q  回复  更多评论
  
# re: 用JAVA轻松操作properties文件
2008-09-16 21:46 | 云飞扬
如果是在web项目里面,该如何配置和获取properties文件呢?  回复  更多评论
  
# re: 用JAVA轻松操作properties文件
2008-09-18 06:51 | edwardpro
都不finally的,运行几次就把io挂得死死的了.out少了flush,可能会有写不进去的问题的.  回复  更多评论
  
# re: 用JAVA轻松操作properties文件
2008-11-19 15:23 | succ800
对于初学的我来说,一个都看不懂,就知道几个单词
  回复  更多评论
  
# re: 用JAVA轻松操作properties文件
2009-01-09 13:52 | 18w
@succ800
你不錯了還看得懂單詞.。  回复  更多评论
  
# re: 用JAVA轻松操作properties文件
2009-03-19 14:53 | 雨帝夜泪
哈哈,谢谢,刚好需要这方面的内容  回复  更多评论
  
# re: 用JAVA轻松操作properties文件
2010-05-27 20:19 | bavol
支持  回复  更多评论
  
# re: 用JAVA轻松操作properties文件
2010-09-16 10:19 | mick
中文乱码  回复  更多评论
  
# re: 用JAVA轻松操作properties文件
2011-02-25 13:48 | ChardRapid
IO流都不关?这样的代码好好斟酌一下  回复  更多评论
  
# re: 用JAVA轻松操作properties文件
2011-07-05 00:02 | Ghost
谢谢  回复  更多评论
  
# re: 用JAVA轻松操作properties文件
2011-08-25 14:43 |
@ChardRapid
你真2,人家只是给你个例子。  回复  更多评论
  
# re: 用JAVA轻松操作properties文件
2012-05-25 15:01 | pengguohui
@云飞扬
以下估计是你想要的答案。
/**
* @author PENGGUOHUI
* @explain以下方法是通过单例模式以及应用反射机制取得属性文件值。
* @date 2012-5-25
*/
private static Properties properties = new Properties();
static{
try {
PoiWordTest.class.getClassLoader().getResourceAsStream("application.properties");//通过反射机制取得WEB工程ClassPath下属性文件
} catch (Exception e) {
e.printStackTrace();
}
}
public static String readURL(String key){
return (String)properties.get(key);
}  回复  更多评论
  
# re: 用JAVA轻松操作properties文件
2012-05-25 15:12 | pengguohui
@pengguohui
忘记给load了。
重新添上代码:
/**
* @author PENGGUOHUI
* @explain以下方法是通过单例模式以及应用反射机制取得属性文件值。
* @date 2012-5-25
*/
private static Properties properties = new Properties();
static{
try {
properties.load(PoiWordTest.class.getClassLoader().getResourceAsStream("application.properties"));//通过反射机制取得WEB工程ClassPath下属性文件
} catch (Exception e) {
e.printStackTrace();
}
}
public static String readURL(String key){
return (String)properties.get(key);
}  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: