引入:
资源文件可以看成是配置文件,一般存在的形式有两种:properties文件形式和xml文件形式。
资源文件的作用:解决硬编码问题【代码逻辑中写死的代码】,如在使用数据库的时候,一般将数据库连接的四大金刚配置在properties文件中。
properties文件中数据的存储是以键值对的形式存在,每一行为一条数据,只能存储字符串形式的数据,Properties文件中值的部分任意字符都会被当做值的一部分,包括空格。在文件中的存放形式如下图
关于配置文件的存放路径
配置文件存放在项目中的resources资源文件夹中(该文件夹类型是source folder类型)。
关于解析properties文件的四种方式
这几种方式的区别主要集中在流的获取不同。
//为了测试下面几种方式,我先把配置信息配置到properties文件中去
@Test
public void outProperties() throws Exception {
//创建Properties对象pt1
Properties pt1 = new Properties();
pt1.setProperty("User", "root");
pt1.setProperty("PWD", "123456");
try {
PrintStream ps = new PrintStream("resources/mysql2.properties");
pt1.list(ps);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
1.传统的IO流方式
@Test
public void test1() throws Exception {
Properties pt1 = new Properties();
try {
FileInputStream fis = new FileInputStream("resources/mysql2.properties");
BufferedInputStream bis= new BufferedInputStream(fis);
//从输入字节流中读取属性列表(键和元素对)
pt1.load(bis);
//打印输出读取的内容
System.out.println("User : " + pt1.getProperty("User"));
System.out.println("Pwd :" + pt1.getProperty("PWD"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
2.当前类字节码对象获取流
public class GetPropertiesTest002 {
@Test
public void testName() throws Exception {
/**
* 1. 通过当前类字节码文件的方式,一般是用当前类的字节码文件
当前类.class.getResourceAsStream("/文件路径/文件名");
如果是resource文件夹,直接写文件名,如果是普通文件夹写文件夹名/文件名
*/
Properties pt1 = new Properties();
try(
//获取文件流:通过当前类字节码文件的方式,一般是用当前类的字节码文件
InputStream is = GetPropertiesTest002.class.getResourceAsStream("/mysql2.properties");
) {
//通过is流下载键和元素
pt1.load(is);
//打印键和元素
System.out.println(pt1.getProperty("User"));
System.out.println(pt1.getProperty("PWD"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
3.当前类加载器获取流
public class GetPropertiesTest003 {
@Test
public void test3() throws Exception {
/**
* 2. 通过类加载器的方式
1.同当前类的加载器获取
1.当前类.class.getClassLoader();//获取当前类加载器
2.classLoader.getResourceAsStream("文件路径/文件名");//通过类加载器获取流对象,如果是源文件夹,直接文件名
*/
Properties pt1 = new Properties();
ClassLoader classLoader = GetPropertiesTest3.class.getClassLoader();
try(
//获取文件流:通过当前类的加载器获取
InputStream resourceAsStream = classLoader.getResourceAsStream("mysql2.properties");
//包装成缓冲流
BufferedInputStream bufferedInputStream = new BufferedInputStream(resourceAsStream);
) {
//通过is流下载键和元素
pt1.load(bufferedInputStream);
//打印键和元素
System.out.println(pt1.getProperty("User"));
System.out.println(pt1.getProperty("PWD"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
4.当前线程的类加载器获取流
@Test
public void analyticalProperties() throws Exception {
/**
* 2.通过当前线程的类加载器获取流
1、ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); //获取当前线程类加载器
2、InputStream is = classLoader.getResourceAsStream("文件路径/文件名");//通过当前线程的类加载器获取流对象,如果是源文件夹,直接文件名
*/
//创建Properties对象pt
Properties pt1 = new Properties();
//获取当前线程
Thread currentThread = Thread.currentThread();
//获取当前线程类加载器
ClassLoader contextClassLoader = currentThread.getContextClassLoader();
try(
//获取文件流:当前线程类加载器获取
InputStream resourceAsStream = contextClassLoader.getResourceAsStream("mysql2.properties");
//包装成缓冲流
BufferedInputStream bufferedInputStream = new BufferedInputStream(resourceAsStream);
) {
//通过is流下载键和元素
pt1.load(bufferedInputStream);
//打印键和元素
System.out.println(pt1.getProperty("User"));
System.out.println(pt1.getProperty("PWD"));
} catch (IOException e) {
e.printStackTrace();
}
}