java-初识Properties

1.通过代码了解一哈:

 1 package com.etc;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileNotFoundException;
 6 import java.io.FileOutputStream;
 7 import java.io.IOException;
 8 import java.util.Properties;
 9 /*
10  * 作用:读写资源配置文件
11  * 方法:
12  * 1.存取方法
13  * setProperty(String key,String value);键与值只能为字符串
14  * getProperty(String key);存在则返回值,不存在返回空
15  * getProperty(String key,defaultValue);如果不存在返回默认值defaultValue
16  * 2.存储到文件中
17  * (1)生成的文件后缀名为.properties;
18  * store(OutputStream out,String comments);
19  * store(Writer writer,String comments);
20  * (2)生成的文件后缀名为.xml
21  * storeToXML(OutputStream out,String comments);默认UTF-8字符集
22  * storeToXML(OutputStream out,String comments,String encoding);指定字符集
23  * 3.从文件中读取内容
24  * (1).properties文件读取
25  * load(InputStream inStream);
26  * load(Reader reader)
27  * (2).xml文件读取
28  * loadFromXML(InputStream inStream)
29  * 4.相对路径与绝对路径:
30  * 相对路径:当前项目,工程
31  * 绝对路径:具体盘符
32  * 5.类路径加载资源文件
33  * 类所在的根路径uri:
34  * (1)类名.class.getResourceAsStream("/uri");
35  * (2)Thread.currentThread().getContextClassLoader().getResourceAsStream("/uri");
36  */
37 public class TestProperties {
38 
39     public static void main(String[] args) throws FileNotFoundException, IOException {
40 
41         //利用Properties存取信息,以后可以用来改进连接数据库的方法,提高效率
42         Properties pro=new Properties();
43         //注册驱动
44         pro.setProperty("driver", "com.mysql.jdbc.Driver");
45         //获取连接:url:jdbc:mysql://连接主机IP:端口号/数据库名字
46         pro.setProperty("url", "jdbc:mysql://localhost:3306/TEST");
47         //用户名与密码
48         pro.setProperty("password", "121515");
49         pro.setProperty("user", "root");
50         //获取value
51         System.out.println(pro.getProperty("url","空"));
52         System.out.println(pro.getProperty("test","空"));
53         //存储到桌面
54         pro.store(new FileOutputStream(new File("C:/Users/Administrator/Desktop/文件1.properties")), "ABC");
55         pro.storeToXML(new FileOutputStream(new File("C:/Users/Administrator/Desktop/文件2.xml")), "测试文件","GBK");
56         pro.storeToXML(new FileOutputStream(new File("文件3.xml")), "测试文件","GBK");
57         
58         //从存储文件中获取信息并存入pro2对象中
59         Properties pro2=new Properties();
60         pro2.load(new FileInputStream (new File("C:/Users/Administrator/Desktop/文件1.properties")));
61         System.out.println("文件读取内容为:"+pro2.getProperty("url")); 
62     }
63 
64 }

2.效果截图:

控制台输出:

项目:

桌面:

打开文件:

接下来利用面向对象的理念实现一个教师信息写入文件的小案例:

1.新建一个实体类,用于存放数据

 1 package com.test;
 2 //实体类用于存放数据
 3 public class Teacher {
 4     private String name;
 5     private int id;
 6     private String date;
 7 
 8     public Teacher(String name, int id, String date) {
 9         super();
10         this.name = name;
11         this.id = id;
12         this.date = date;
13     }
14     public String getName() {
15         return name;
16     }
17     public void setName(String name) {
18         this.name = name;
19     }
20     public int getId() {
21         return id;
22     }
23     public void setId(int id) {
24         this.id = id;
25     }
26     public String getDate() {
27         return date;
28     }
29     public void setDate(String date) {
30         this.date = date;
31     }
32     @Override
33     public String toString() {
34         return  "姓名:"+name+" 编号:"+id+" 入教时间:"+date;
35     }
36 
37 }

2.测试类,场景模拟

 1 package com.test;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileNotFoundException;
 6 import java.io.FileOutputStream;
 7 import java.io.IOException;
 8 import java.util.Properties;
 9 
10 /*
11  * 简单运用面向对象实现教师信息写入配置文件加以保存
12  * 总共分为两步:
13  * 1.输入数据
14  * 2.处理数据并将其存储在桌面的文件下
15  */
16 public class teacherInfoWriteIn {
17 
18     public static void main(String[] args) throws FileNotFoundException, IOException {
19         //获取数据并将数据存取至桌面的文件1.xml文件中
20         Properties pro=setData();
21         pro.storeToXML(new FileOutputStream(new File("C:/Users/Administrator/Desktop/Test1.xml")), "教师信息表","GBK");
22         //将桌面上的文件1里面的信息获取出来
23         Properties pro2=new Properties();
24         pro2.loadFromXML(new FileInputStream(new File("C:/Users/Administrator/Desktop/Test1.xml")));
25         System.out.println(pro2.getProperty("张老师信息"));
26         System.out.println(pro2.getProperty("李老师信息"));
27         System.out.println(pro2.getProperty("王老师信息"));
28         System.out.println(pro2.getProperty("刘老师信息"));
29     }
30     //数据初始化输入
31     public  static Properties setData() {
32         Properties pro=new Properties();
33         Teacher t1=new Teacher("张三",11152,"2004-5");
34         Teacher t2=new Teacher("李四",13157,"2007-4");
35         Teacher t3=new Teacher("王五",12456,"1998-5");
36         Teacher t4=new Teacher("刘六",15478,"1999-5");
37         pro.put("张老师信息",t1.toString());
38         pro.put("李老师信息",t2.toString());
39         pro.put("王老师信息",t3.toString());
40         pro.put("刘老师信息",t4.toString());
41         return pro;
42     }
43 }

效果截图:

控制台:

桌面:

ps:文章待完善,先简单了解学习一下,如有问题欢迎大佬指点。

原文地址:https://www.cnblogs.com/weekstart/p/10782730.html