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;
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){
String[] split = line.split("=");
System.out.println(split[0]+ " " + split[1]);
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;
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);
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;
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);
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;
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");
properties.store(new FileWriter(filePath),null);
System.out.println("添加到新的文件成功!!!");
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 = new Properties();
properties.setProperty("charset", "utf8");
properties.setProperty("user", "汤姆");
properties.setProperty("pwd", "888888");
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;
public class HomeWork01 {
public static void main(String[] args) throws IOException {
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);
if (file1.exists()){
System.out.println("hello.txt 文件已经存在!!!");
}else {
if (file1.createNewFile()) {
System.out.println("hello.txt 文件创建成功!!!");
} else {
System.out.println("hello.txt 文件创建失败!!!");
}
}
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.*;
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
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;
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();
}
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("src\\IO_\\home_\\dog.dat"));
oos.writeObject(dog);
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;
}
}
|