`
newchq
  • 浏览: 53434 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

JAVA中如何读写properties文件

阅读更多

       Properties文件是JAVA程序特有的一种文件类型,信息以"键--值"的格式进行存放,通常将容易变化的系统参数写到此文件中。这样就可以实现快速的读和写。具体的操作方法:

读操作:

 

  public String readpro(String filename,String key){

             Properties props = new Properties();

             try{

                     props.load(this.getClass().getClassLoader().getResourceAsStream(filename));

                     String value = props.getProperty(key);

                     return value;

             }catch(){

                     return null;

             }

 }

 全部读取的方法:

   

Enumeration en = new props.propertyNames();

    while(en.hasMoreElements()){

             String key = (String)en.nextElement();

             String value = props.getProperty(key);

            //其他相关操作

    }

 

写操作:

public void writeProps(String filename,String name,String value){

          Properties props = new Properties();

         String path = this.getClass().getClassLoader().getResource("").getPath().substring(1);

         path=path+filename;

         try{

                  InputStream fis = new InputStream(path);

                  props.load(fis);    //从输入流中读取属性列表

                  OutputStream fos = new OutputStream(path);

                  props.setProperty(name,value);

                  props.stare(fos,"Update' "+name+" 'value");

         }catch(Exception e){

         }

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics