Properties是什么?
可以将起作用与map相结合,是map体系中的集合类。
其作用是为了让用户够脱离程序本身去修改相关的变量设置。该类主要用于读取Java的配置文件,配置文件中很多变量是经常改变的,为了方便用户的配置,就像在Java中是以键值对的形式进行参数配置的。
properties作为map集合的特有方法
setProperty(String key,String value)
//设置集合的键和值,底层是在调用Hashable的put方法
putProperty(String key)
//根据key值搜索相对应的value并返回
stringPropertyNames()
//从属性列表返回一个不可更改的键集
应用举例
package IO_operation.IOStream_study;
import java.util.Properties;
import java.util.Set;
public class demo011 {
public static void main(String[] args) {
//创建集合对象
Properties prop = new Properties();
//向集合中添加属性值
prop.setProperty("ID001","张三");
prop.setProperty("ID002","李四");
prop.setProperty("ID003","王五");
//根据指定的键搜索值
System.out.println(prop.getProperty("ID002"));
//直接输出集合中的全部数据
System.out.println(prop);
//返回不可修改的键集
Set<String> names = prop.stringPropertyNames();
for(String key : names){
String value = prop.getProperty(key);
System.out.println(key+","+value);
}
}
}
properties和IO流的结合使用
load(Reader Reader)
//从字符流读取属性列表
store(Writer writer,String comments)
//将属性列表中的数据写入properties表中
对于store可以看Java Store_java之Properties集合中的方法store-CSDN博客
应用举例
package IO_operation.IOStream_study;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
public class demo012 {
public static void main(String[] args) throws IOException {
myStore();
myLoad();
}
private static void myLoad() throws IOException {
Properties prop = new Properties();
FileReader fr = new FileReader("cc\\fw.txt");
prop.load(fr);
fr.close();
System.out.println(prop);
}
private static void myStore() throws IOException {
Properties prop = new Properties();
prop.setProperty("ID001","张三");
prop.setProperty("ID002","李四");
prop.setProperty("ID003","王五");
FileWriter fw = new FileWriter("cc\\fw.txt");
prop.store(fw,null);
fw.close();
}
}
综合案例分析
在环境目录下创建prop.properties文件,文件内容如下:
name=jia
age=20
案例要求及代码如下:
package IO_operation.IOStream_study;
//案例说明:在Properties文件中手动写上姓名、年龄,读取到集合中,将该数据封装成学生对象,写到本地文件中
import java.io.*;
import java.util.Properties;
public class demo013 {
public static void main(String[] args) throws IOException {
//创建Properties集合,将本地文件数据加载到集合中
Properties prop = new Properties();
FileReader fr = new FileReader("prop.properties");
// InputStreamReader fr = new InputStreamReader(new FileInputStream("prop.properties"), "GBK");
//问题:在这里使用InputStreamReader或者FileReader,输出的中文都为乱码,可能和使用的编码解码方式有关,待解决
prop.load(fr);
fr.close();
//获取到键值对数据封装到学生对象中
String name = prop.getProperty("name");
int age = Integer.parseInt(prop.getProperty("age"));//将字符串类型的数据改成整型数据
Student s = new Student(name,age);
//创建序列化对象,将学生对象序列化到本地文件中
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("a.txt"));
oos.writeObject(s);
oos.close();
System.out.println(s.getName()+","+s.getAge()+"岁");
}
}
关于IO流的总结
//创建文件,但不会创建目录
File f1 = new File("cc\\ss");
System.out.println(f1.mkdirs());
File f2 = new File(f1,"gg.txt");
System.out.println(f2.createNewFile());
//字节流的输入(read)和输出(write)
//在所有的输出或写文件之前,若文件不存在系统会创建文件,若文件存在系统会将文件清空
FileOutputStream f1 = new FileOutputStream("cc\\fos.txt");
FileInputStream f1 = new FileInputStream("cc\\fos.txt");
//字节缓冲流的输入和输出、可以避免多次系统调用
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("cc\\Demo_video.mp4"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("cc\\copy_video.mp4"));
//字符流的输入和输出、突出表现在对于汉字的编码和解码
FileWriter fw = new FileWriter("name&&password.txt");
FileReader fr = new FileReader("name&&password.txt");
//字符缓冲流
BufferedReader br = new BufferedReader(new FileReader("cc\\fos.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("cc\\fos.txt"));