配置文件类 Properties

Properties(配置文件类): 主要用于生产配置文件与读取配置文件的信息。

Properties属于集合类,继承于Hashtable。

Properties要注意的细节:
    1. 如果配置文件的信息一旦使用了中文,那么在使用store方法生成配置文件的时候只能使用字符流解决,如果使用字节流生成配置文件的话,默认使用的是iso8859-1码表进行编码存储,这时候会出现乱码。
    2. 如果Properties中的内容发生了变化,一定要重新使用Properties生成配置文件,否则配置文件信息不会发生变化。

常用方法:

1、构造方法:

Properties() 创建一个无默认值的空属性列表。

Properties(Properties defaults) 创建一个带有指定默认值的空属性列表。

2、常用方法

void load(InputStream inStream) 从输入流中读取属性列表(键和元素对)。

void load(Reader reader) 按简单的面向行的格式从输入字符流中读取属性列表(键和元素对)。

void  store(OutputStream out, String comments) 以适合使用 load(InputStream) 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。

void  store(Writer writer, String comments) 以适合使用 load(Reader) 方法的格式,将此 Properties 表中的属性列表(键和元素对)写入输出字符。

Object setProperty(String key, String value) 调用 Hashtable 的方法 put

String getProperty(String key) 用指定的键在此属性列表中搜索属性。

实战:使用properties实现本软件只能 运行三次,超过了三次之后就提示购买正版并推出jvm。

代码示例如下

 1 import java.io.File;
 2 import java.io.FileReader;
 3 import java.io.FileWriter;
 4 import java.io.IOException;
 5 import java.util.Properties;
 6 
 7 
 8 public class DemoProperties {
 9     
10     public static void main(String[] args) throws IOException {
11         File file = new File("E:\nick.property");
12         if (!file.exists()) {
13             file.createNewFile();
14         }
15         Properties properties = new Properties();
16         properties.load(new FileReader(file));
17         int count = 0;
18         String value = properties.getProperty("登录次数");
19         if (value!=null ) {
20             count = Integer.parseInt(value);
21         }
22         count++;
23         if (count>=3) {
24             System.out.println("试用期已过,请购买正版。");
25             System.exit(0);
26         }
27         
28         properties.setProperty("登录次数", count+"");
29         properties.store(new FileWriter(file), "登录次数"+count);
30     }
31 
32 }
View Code

Properties路径配置

为了解决程序中经常发生改变数据的配置问题,可以采用Properties文件来处理。从而在不改变原有程序的基础上实现数据的改变。

常见路径:

  绝对路径:一个文件的完整路径信息。一般绝对路径是包含有盘符的。  绝对路径的缺陷: 因为绝对路径是有盘符开头的,有些系统是没有盘符的。
     
     相对路径: 相对路径是相对于当前程序的路径。当前路径就是执行java命令的时候,控制台所在的路径。
     
     类文件路径 :类文件路径就是使用了classpath的路径找对应的资源文件。

配置Properties文件路径采用类文件路径是比较好的选择。如果需要使用到类文件路径首先先要获取到一个Class对象,并通过class对象的

输入字符流 getResourceAsStream(路径字符串)来load Properties文件。

代码示例

 1 public class DBUtil {
 2     
 3     static Properties properties ;
 4     
 5     static{
 6         try {
 7             properties = new Properties();
 8             //去加载配置文件  /
 9             Class clazz = DBUtil.class; 
10             InputStream inputStream = clazz.getResourceAsStream("/db.properties"); //  "/"代表了Classpath的路径。           getResourceAsStream 该方法里面使用的路径就是使用了类文件路径。
11             properties.load(inputStream);
12         } catch (IOException e) {
13             // TODO Auto-generated catch block
14             e.printStackTrace();
15         }
16     }
17     
18     public static void main(String[] args) {
19         System.out.println("当前路径:"+ new File(".").getAbsolutePath() );
20         System.out.println("用户名:"+ properties.getProperty("userName")+" 密码:"+properties.getProperty("password"));
21         
22     }
原文地址:https://www.cnblogs.com/nicker/p/6253536.html