Properties
类【重点】
看一个需求:
如下一个配置文件 mySql.properties 文件
ip = 192.127.0.1
user = root
passwd = 123456
请问编程读取 ip user 和 pwd
的值是多少
分析:
- 先使用传统方法
- 使用
Propertyies
类可以方便实现
使用传统方法
package IO_.properties_;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
* @author: 海康
* @version: 1.0
*/
public class Properties1 {
public static void main(String[] args) throws IOException {
String filePath = "src\\mySql.properties";
BufferedReader read = new BufferedReader(new FileReader(filePath));
String line = "";
while ((line=read.readLine())!=null){
// System.out.println(line);
String[] split = line.split("=");
System.out.println(split[0]+ " " + split[1]);
// 只获取 IP
if (split[0].equals("ip")){
System.out.println(split[1]);
}
}
}
}
使用Properties
类
package IO_.properties_;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
/**
* @author: 海康
* @version: 1.0
*/
public class Properties2 {
public static void main(String[] args) throws IOException {
String filePath = "src\\mySql.properties";
Properties properties = new Properties();
BufferedReader bf = new BufferedReader(new FileReader(filePath));
properties.load(bf);
// 获取相关的 value 值
String ip = properties.getProperty("ip");
System.out.println(ip);
String username = properties.getProperty("username");
System.out.println(username);
String passwd = properties.getProperty("passwd");
System.out.println(passwd);
}
}
基本介绍
1.专门用于读写配置文件的集合类
配置文件的格式:
键=值
2.注意是:键值对不需要有空格,值不需要用引号一起来。默认类型是String
3.Properties
的常用方法
1.load : 加载配置文件的键值对到Properties对象
2.list : 将数据显示到指定设置中
3.getProperty(key) : 根据键获取值
4.setProperty(key,value) : 设置键值对到Properties对象,如果在没有key值,相当于添加了
5.store(writer,String) : 将Properties中的键值对存储到配置文件中,在idea中,保存信息到配置文件中,如果含有中文,会存储为unicode码,如果已经存在该文件相当于覆盖,如果没有就创建,注意是第二参数,如果传入参数表示在最上边显示注释,一般传入null值
应用案例
1.使用Properties
类完成对 mySql.properties
的读取
package IO_.properties_;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
/**
* @author: 海康
* @version: 1.0
*/
public class Properties2 {
public static void main(String[] args) throws IOException {
String filePath = "src\\mySql.properties";
Properties properties = new Properties();
BufferedReader bf = new BufferedReader(new FileReader(filePath));
properties.load(bf);
// 获取相关的 value 值
String ip = properties.getProperty("ip");
System.out.println(ip);
String username = properties.getProperty("username");
System.out.println(username);
String passwd = properties.getProperty("passwd");
System.out.println(passwd);
}
}
2.使用Properties
类添加key-val
到新文件mySql2.properties
中
3.使用Properties
类完成对 mySql2.properties
的读取,并修改某个key-value
值
package IO_.properties_;
import java.io.*;
import java.util.Properties;
/**
* @author: 海康
* @version: 1.0
*/
public class Properties3 {
public static void main(String[] args) throws IOException {
String filePath = "src\\mySql2.properties";
Properties properties = new Properties();
properties.setProperty("user","eeje");
properties.setProperty("pwd","123456");
// 第二个参数表示是 注释,一般传入 null值,表示不用注释,如果传入值则在最上面显示注释
properties.store(new FileWriter(filePath),null);
System.out.println("添加到新的文件成功!!!");
// 修改一个 key - value 的值,可以存在则修改,不存在则,添加
properties.setProperty("user","haikang");
System.out.println("修改成功!!!");
}
}
package com.hspedu.properties_;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class Properties03 {
public static void main(String[] args) throws IOException {
//使用Properties 类来创建 配置文件, 修改配置文件内容
Properties properties = new Properties();
//创建
//1.如果该文件没有key 就是创建
//2.如果该文件有key ,就是修改
/*
Properties 父类是 Hashtable , 底层就是Hashtable 核心方法
public synchronized V put(K key, V value) {
// Make sure the value is not null
if (value == null) {
throw new NullPointerException();
}
// Makes sure the key is not already in the hashtable.
Entry<?,?> tab[] = table;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
@SuppressWarnings("unchecked")
Entry<K,V> entry = (Entry<K,V>)tab[index];
for(; entry != null ; entry = entry.next) {
if ((entry.hash == hash) && entry.key.equals(key)) {
V old = entry.value;
entry.value = value;//如果key 存在,就替换
return old;
}
}
addEntry(hash, key, value, index);//如果是新k, 就addEntry
return null;
}
*/
properties.setProperty("charset", "utf8");
properties.setProperty("user", "汤姆");//注意保存时,是中文的 unicode码值
properties.setProperty("pwd", "888888");
//将k-v 存储文件中即可
properties.store(new FileOutputStream("src\\mysql2.properties"), null);
System.out.println("保存配置文件成功~");
}
}
本章作业
package IO_.home_;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
/**
* @author: 海康
* @version: 1.0
*/
public class HomeWork01 {
public static void main(String[] args) throws IOException {
// 1.判断E盘下是否有文件夹 mytemp 如果没有创建 mytemp
String filePath = "e:\\mytemp";
File file = new File(filePath);
if (file.exists()){
System.out.println("该文件夹已经存在!!!");
}else {
if (file.mkdir()){
System.out.println("该文件夹创建成功!!!");
}else {
System.out.println("该文件夹创建失败!!!");
}
}
String crateFile = "e:\\mytemp\\helo.txt";
File file1 = new File(crateFile);
// 如果 hello.txt 已经存在,提示该文件已经存在,就不要再重复创建了
if (file1.exists()){
System.out.println("hello.txt 文件已经存在!!!");
}else {
// 2.在 e:\\mytemp 目录下,创建文件 hello.txt
if (file1.createNewFile()) {
System.out.println("hello.txt 文件创建成功!!!");
} else {
System.out.println("hello.txt 文件创建失败!!!");
}
}
// 并且在 hello.txt 文件中 写入 hello world~~~
BufferedWriter bw = new BufferedWriter(new FileWriter(file1));
bw.write("hello world~~~");
bw.newLine();// 写入一个换行
// 在使用完成流后一定关闭流
if (bw != null){
bw.close();
}
}
}
package IO_.home_;
import org.junit.jupiter.api.Test;
import java.io.*;
/**
* @author: 海康
* @version: 1.0
*/
public class HomeWork02 {
public static void main(String[] args) throws IOException {
String filePath = "e:\\IDEACODE\\javase\\new.txt";
BufferedReader br = new BufferedReader(new FileReader(filePath));
String line = "";
while ((line = br.readLine())!=null){
System.out.println(line+" ,");
}
// 在使用完流后一定要关闭流
if (br != null){
br.close();
}
}
@Test
/**
* 将 utf-8 改成 gbk 出现 中文乱码问题,并解决
*/
public void read() throws IOException {
String filePath = "e:\\IDEACODE\\javase\\new.txt";
InputStreamReader isp = new InputStreamReader(new FileInputStream(filePath),"gbk");
BufferedReader br = new BufferedReader(isp);
String line = "";
while ((line=br.readLine())!=null){
System.out.println(line+" ,");
}
// 在使用流后一定要关闭
if (br != null){
isp.close();
}
}
}
package IO_.home_;
import java.io.*;
import java.util.Properties;
/**
* @author: 海康
* @version: 1.0
*/
public class HomeWork3 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
String filePath = "src\\IO_\\home_\\dog.properties";
Properties properties = new Properties();
BufferedReader br = new BufferedReader(new FileReader(filePath));
properties.load(br);
// 读取相关信息
String name = properties.getProperty("name");
String age = properties.getProperty("age");
Integer integer = new Integer(age);
String color = properties.getProperty("color");
Dog dog = new Dog(name, integer, color);
System.out.println(dog.toString());
// 用完流一定要释放流
if (br != null){
br.close();
}
// 将创建的Dog对象,序列化到文件dog.dat文件中
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("src\\IO_\\home_\\dog.dat"));
oos.writeObject(dog);
// 将文件中dog.dat 中的内容进行反序列化
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("src\\IO_\\home_\\dog.dat"));
Object object = ois.readObject();
Dog dog1 = (Dog) object;
System.out.println("狗的名字:" + dog1.getName());
if (oos != null){
oos.close();
}
if (ois != null){
ois.close();
}
}
}
class Dog implements Serializable{
private String name;
private int age;
private String color;
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
", color='" + color + '\'' +
'}';
}
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;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Dog(String name, int age, String color) {
this.name = name;
this.age = age;
this.color = color;
}
}