如何在Java中使用属性类

  介绍

本篇文章为大家展示了如何在Java中使用属性类,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

<强>概念理解

属性继承于Hashtable。表示一个持久的属性集,属性列表以键-值的形式存在,键和值都是字符串. Properties类被许多Java类使用,例如,在获取环境遍历时它就作为System.getProperties()方法的返回值。我们在很多需要避免硬编码的应用场景下需要使用属性文件来加载程序需要配置的信息,比如JDBC、MyBatis框架等. Properties类则是属性文件和程序的中间桥梁,不论是属性从文件读取信息还是写入信息到属性文件都要经由属性类。

<强>写入

属性类调用setProperty方法将键值对保存到内存中,此时可以通过getProperty方法读取,propertyNames方法进行遍历,但是并没有将键值对持久化到属性文件中,故需要调用存储方法持久化键值对到属性文件中。

我们写一个类测试

import  java.io.FileNotFoundException;   import  java.io.FileOutputStream;   import  java.io.IOException;   import  java.io.OutputStream;   import  java.util.Date;   import  java.util.Properties;      public  class  TestProperties  {   public  void  writeProperties (), {   Properties  Properties =, new 属性();   OutputStream  output =,空;   try  {   时间=output  new  FileOutputStream (“config.properties");   properties.setProperty (“url",,“jdbc: mysql://localhost: 3306/?;   properties.setProperty (“username",,“root");   properties.setProperty (“password",,“root");   properties.setProperty (“databases",,“music_player");   properties.store(输出,“Steven1997  modify", +, new 日期().toString ());   },catch  (IOException  e), {   e.printStackTrace ();   }finally  {   如果(输出!=null), {   try  {   output.close ();   }catch  (IOException  e), {   e.printStackTrace ();   }   }   }      }   public  static  void  main (String [], args), {   TestProperties  t =, new  TestProperties ();   t.writeProperties ();   }   }

执行后,工程下面会出现一个配置。属性文件,属性文件内容如下:

如何在Java中使用财产类”> </p> <p> <强>读取</强> </p> <p>使用getProperty获取配置。属性文件配置文件的各项属性。</p> <pre类= package 产权;      import  java.io.FileInputStream;   import  java.io.FileNotFoundException;   import  java.io.FileOutputStream;   import  java.io.IOException;   import  java.io.InputStream;   import  java.util.Properties;      public  class  LoadProperties  {   public  void  loadProperties (), {   Properties  Properties =, new 属性();   InputStream  InputStream =,空;      try  {   时间=inputStream  new  FileInputStream (“config.properties");   properties.load (inputStream);   System.out.println (“url:“, +, properties.getProperty (“url"));   System.out.println(“用户名:“,+,properties.getProperty (“username"));   System.out.println(“密码:“,+,properties.getProperty (“password"));   System.out.println(“数据库:“,+,properties.getProperty (“database"));   },catch  (IOException  e), {   e.printStackTrace ();   }finally  {   如果(inputStream  !=null), {   try  {   inputStream.close ();   },catch  (IOException  e), {   e.printStackTrace ();   }   }   }      }   public  static  void  main (String [], args), {   LoadProperties  l =, new  LoadProperties ();   l.loadProperties ();   }   }

运行后的结果

url: jdbc: mysql://localhost: 3306/
用户名:根
密码:根
数据库:music_player

<>强遍历

遍历属性文件中的键值对

package 产权;      import  java.io.InputStream;   import 活动;   import  java.util.Map.Entry;   import  java.util.Properties;   import  java.util.Set;      public  class  PropertiesTest  {   public  void  printAll (), {   Properties  prop =, new 属性();   InputStream  input =,空;   try  {   String  file =,“config.properties";   .getClassLoader input =, getClass () () .getResourceAsStream(文件);   如果(input ==, null), {   System.out.println(“无法加载文件“,+,文件);   return ;   }   prop.load(输入);//,方法一   Set

如何在Java中使用属性类