properties类和序列化/反序列化
一、properties类
Hashtable的子类,map的子类,无序,key value值均为string
创建properties集合:
Properties prot=new Properties();
存数据:put
prot.put("aa","bb");
取数据:getProperty
prot.getProperty("aa");
配置文件的读取: .load
fis=new FileInputStream("src/aaa/bbb/ccc/pro.properties"); //数据源
将键值对读取到properties集合对象中
prot.load(fis)
配置文件的写入: .store
fos=new FileOutputStream("src/aaa/bbb/ccc/pro.properties"); //目的地
pro.store(fos,"注释");
序列化与反序列化
序列化:将对象写入文件中,需要实体类实现Serializable才可执行序列化。
1.创建实体类的Person对象p
2.明确目的地:FileOutputStream fos=new FileOutputStream("d:\\aa\\bb\\person.txt")
3.创建序列化流ObjectOutputStream oos=new ObjectOutputStream(fos)
4.讲对象写入文件 p.writeObject(p)
5.释放资源oos.close()
反序列化:从文件中读取对象
1.明确数据源:FileInputStream fis=new FileInputStream("d:\\aa\bb\person.txt")
2.创建反序列化流: ObjectInputStream ois=new ObjectInputStream(fis)
3.从文本中读取对象ois.readObject() ,为object类型
4.向下转型并存储Person p=(Person)ois.readObject()
5.释放资源ois.close()
6.clear(),清除所有装载的键 - 值对
二、properties配置文件,读取本地数据
//明确数据源,将指定的配置文件读入到文件输入流 FileInputStream fis=new FileInputStream("D:\\java1018\\b\\pro.properties"); //创建Properties对象 Properties pro= new Properties(); //从输入流中读取文件的配置信息,放入pro中 pro.load(fis); //打印测试 System.out.println(pro);
三、properties配置文件,存值与取值
//创建Properties对象 Properties p=new Properties(); //存值(键值对) p.setProperty("1", "小红帽"); p.setProperty("2", "大灰狼"); p.setProperty("3", "狼外婆"); //取值 System.out.println(p.getProperty("1"));
四、序列化流和反序列化流
1.创建person类
public class Person implements Serializable{ private String name; //private transient int age;//瞬态修饰符 //private static int age;//静态修饰的成员变量属于类,序列化流只能序列换属于对象的属性 private static int age; public static final long serialVersionUID=1L; @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } public Person(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
2.序列化
public class Demo01 { public static void main(String[] args) throws IOException { // 明确数据源 FileOutputStream fos = new FileOutputStream("D:\\java1018\\person.txt"); // 创建序列化流对象 ObjectOutputStream oos = new ObjectOutputStream(fos); //创建集合 ArrayList<Person> arr=new ArrayList<Person>(); // 创建对象 Person p = new Person("张三", 18); Person p1 = new Person("李四", 16); //向集合中添加对象 arr.add(p); arr.add(p1); // 写入对象 oos.writeObject(arr); // 释放资源 oos.close(); } }
3.反序列化
public class Demo02 { public static void main(String[] args) throws IOException, ClassNotFoundException { //明确数据源 FileInputStream fis=new FileInputStream("D:\\java1018\\person.txt"); //创建反序列化流对象 ObjectInputStream ois=new ObjectInputStream(fis); //反序列化得到对象 Object o=ois.readObject(); System.out.println(o); //释放资源 ois.close(); } }
五、properties小应用
1.创建db.properties(链接)
https://jingyan.baidu.com/article/6525d4b18b1fbfac7d2e9431.html
内容:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/java 1018?characterEncoding=UTF-8
user=root
password=123456
2.注册驱动
public class JDBCUtils { //获取connection对象的方法 public static Connection getconn() { //1.注册驱动 Connection conn=null; try { //明确数据源 FileInputStream fis=new FileInputStream("src/com/oracle/tools/db.properties"); //创建Properties集合 Properties pro=new Properties(); pro.load(fis); Class.forName(pro.getProperty("driver")); //2.连接数据库(获取连接对象) String url=pro.getProperty("url"); String user=pro.getProperty("user"); String password=pro.getProperty("password"); conn = DriverManager.getConnection(url,user,password); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; }

浙公网安备 33010602011771号