1.通过代码了解一哈:
package com.etc; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
/*
* 作用:读写资源配置文件
* 方法:
* 1.存取方法
* setProperty(String key,String value);键与值只能为字符串
* getProperty(String key);存在则返回值,不存在返回空
* getProperty(String key,defaultValue);如果不存在返回默认值defaultValue
* 2.存储到文件中
* (1)生成的文件后缀名为.properties;
* store(OutputStream out,String comments);
* store(Writer writer,String comments);
* (2)生成的文件后缀名为.xml
* storeToXML(OutputStream out,String comments);默认UTF-8字符集
* storeToXML(OutputStream out,String comments,String encoding);指定字符集
* 3.从文件中读取内容
* (1).properties文件读取
* load(InputStream inStream);
* load(Reader reader)
* (2).xml文件读取
* loadFromXML(InputStream inStream)
* 4.相对路径与绝对路径:
* 相对路径:当前项目,工程
* 绝对路径:具体盘符
* 5.类路径加载资源文件
* 类所在的根路径uri:
* (1)类名.class.getResourceAsStream("/uri");
* (2)Thread.currentThread().getContextClassLoader().getResourceAsStream("/uri");
*/
public class TestProperties { public static void main(String[] args) throws FileNotFoundException, IOException { //利用Properties存取信息,以后可以用来改进连接数据库的方法,提高效率
Properties pro=new Properties();
//注册驱动
pro.setProperty("driver", "com.mysql.jdbc.Driver");
//获取连接:url:jdbc:mysql://连接主机IP:端口号/数据库名字
pro.setProperty("url", "jdbc:mysql://localhost:3306/TEST");
//用户名与密码
pro.setProperty("password", "121515");
pro.setProperty("user", "root");
//获取value
System.out.println(pro.getProperty("url","空"));
System.out.println(pro.getProperty("test","空"));
//存储到桌面
pro.store(new FileOutputStream(new File("C:/Users/Administrator/Desktop/文件1.properties")), "ABC");
pro.storeToXML(new FileOutputStream(new File("C:/Users/Administrator/Desktop/文件2.xml")), "测试文件","GBK");
pro.storeToXML(new FileOutputStream(new File("文件3.xml")), "测试文件","GBK"); //从存储文件中获取信息并存入pro2对象中
Properties pro2=new Properties();
pro2.load(new FileInputStream (new File("C:/Users/Administrator/Desktop/文件1.properties")));
System.out.println("文件读取内容为:"+pro2.getProperty("url"));
} }
2.效果截图:
控制台输出:
项目:
桌面:
打开文件:
接下来利用面向对象的理念实现一个教师信息写入文件的小案例:
1.新建一个实体类,用于存放数据
package com.test;
//实体类用于存放数据
public class Teacher {
private String name;
private int id;
private String date; public Teacher(String name, int id, String date) {
super();
this.name = name;
this.id = id;
this.date = date;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
@Override
public String toString() {
return "姓名:"+name+" 编号:"+id+" 入教时间:"+date;
} }
2.测试类,场景模拟
package com.test; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties; /*
* 简单运用面向对象实现教师信息写入配置文件加以保存
* 总共分为两步:
* 1.输入数据
* 2.处理数据并将其存储在桌面的文件下
*/
public class teacherInfoWriteIn { public static void main(String[] args) throws FileNotFoundException, IOException {
//获取数据并将数据存取至桌面的文件1.xml文件中
Properties pro=setData();
pro.storeToXML(new FileOutputStream(new File("C:/Users/Administrator/Desktop/Test1.xml")), "教师信息表","GBK");
//将桌面上的文件1里面的信息获取出来
Properties pro2=new Properties();
pro2.loadFromXML(new FileInputStream(new File("C:/Users/Administrator/Desktop/Test1.xml")));
System.out.println(pro2.getProperty("张老师信息"));
System.out.println(pro2.getProperty("李老师信息"));
System.out.println(pro2.getProperty("王老师信息"));
System.out.println(pro2.getProperty("刘老师信息"));
}
//数据初始化输入
public static Properties setData() {
Properties pro=new Properties();
Teacher t1=new Teacher("张三",11152,"2004-5");
Teacher t2=new Teacher("李四",13157,"2007-4");
Teacher t3=new Teacher("王五",12456,"1998-5");
Teacher t4=new Teacher("刘六",15478,"1999-5");
pro.put("张老师信息",t1.toString());
pro.put("李老师信息",t2.toString());
pro.put("王老师信息",t3.toString());
pro.put("刘老师信息",t4.toString());
return pro;
}
}
效果截图:
控制台:
桌面:
ps:文章待完善,先简单了解学习一下,如有问题欢迎大佬指点。