JavaEE进阶(5) 解析资源文件(配置文件)

资源文件可以看成是配置文件,一般的形式有两种:properties形式和XML形式      路径:一般是写在src目录下面,根目录

properties文件中数据的存储是以键值对的形式存在,每一行为一条数据,只能存储字符串形式的数据
               Map==>Propertis
                           String key -- String value
 注意:Properties文件中值的部分任意字符都会被当做值的一部分,尤其是空格

资源文件的作用:解决硬编码问题【代码逻辑中写死的代码】


那么我们如何去读取资源文件里面的内容呢???    (下面三种方式主要集中在流的获取上) 
方式一 ==》传统IO流方式:使用JDK中的Properties类进行数据读取 

①:创建Properties对象
②:load加载资源文件流
③:读取数据:getProperty(key):返回资源文件中对应key中的值(字符串),若key不存在则返回null

方式二 ==》字节码对象获取流            注意:文件最终必须被编译到class文件存放的位置   

①:创建Properties对象
②:通过字节码文件获得资源  (注意:字节码文件 加载流资源,前必须带/,若资源文件没有在包中,包的路径可以省略)           类名.class.getResourceAsStream(/包路径/文件名)
②:load加载资源文件流
③:读取数据:getProperty(key):返回资源文件中对应key中的值(字符串),若key不存在则返回null

方式三 ==》类加载器获取流   (采取通过线程获得类加载器)

①:创建Properties对象
②:通过当前类的字节码文件得到一个类加载器        
类名.class.getClassLoader()
       通过线程获得类加载器     Thread.currentThread().getContextClassLoader()
③:用类加载器加载流资源
④:load加载资源文件流
⑤:读取数据:getProperty(key):返回资源文件中对应key中的值(字符串),若key不存在则返回null

注意:方式一流资源文件路径是写死的  存在隐编码问题  不推荐使用!
           推荐使用方式三中的线程获取类加载器  读取配置文件!!!


ex1:传统IO流方式:使用JDK中的Properties类进行数据读取 

public class TestProperties {
	
	public static void main(String[] args) throws IOException {
		testReadProperties1();
	}
	//传统IO流方式:使用JDK中的Properties类进行数据读取 
	public static void testReadProperties1() throws IOException {
		//①创建Properties对象:用于读取文件
		Properties properties = new Properties();
		//②创建一个流资源关联磁盘文件(这里使用的是绝对路径)读取配置信息,流资源定位需要绝对路径!!
		FileInputStream fis = new FileInputStream("E:/eclipse-workspace/JavaEE_workspace/Day32_JavaBasic/src/db.properties");
		//③properties加载资源文件流
		properties.load(fis);
		//④读取数据:getProperty(key):返回资源文件中对应key中的值(字符串),若key不存在则返回null
		System.out.println(properties.getProperty("username"));//root
		System.out.println(properties.getProperty("password"));//1
		System.out.println(properties.getProperty("notexits"));//null
	}
	
}

运行结果图:


ex2: 字节码对象获取流

public class TestProperties {

	public static void main(String[] args) throws IOException {
		testReadProperties2();
	}
	// 第二种方式:字节码对象获取流
	public static void testReadProperties2() throws IOException {
		//①创建Properties对象:用于读取文件
		Properties properties = new Properties();
		//②通过字节码文件,获得资源
		//注意:字节码文件 加载流资源,前必须带/,若资源文件没有在包中,包的路径可以省略
		InputStream is = TestProperties.class.getResourceAsStream("/db.properties");
		// 配置文件写在包路径下面
		//InputStream is = TestProperties.class.getResourceAsStream("/com/zhengqing/Demo/db.properties");
		//③properties加载资源文件流
		properties.load(is);
		//④读取数据:getProperty(key):返回资源文件中对应key中的值(字符串),若key不存在则返回null
		System.out.println(properties.getProperty("username"));// root
		System.out.println(properties.getProperty("password"));// 1
		System.out.println(properties.getProperty("notexits"));// null
	}

}

ex3:类加载器获取流   ==》通过当前类的字节码文件得到一个类加载器

public class TestProperties {

	public static void main(String[] args) throws IOException {
		testReadProperties3();
	}
	// 第三种方式:类加载器获取流  ==》通过当前类的字节码文件得到一个类加载器
	public static void testReadProperties3() throws IOException {
		// ①创建Properties对象:用于读取文件
		Properties properties = new Properties();
		// ②当前类的字节码文件==>得到一个类加载器【注意:当前类名如果改了,意味着下面的类名必须改!!】
		ClassLoader classLoader = TestProperties.class.getClassLoader();
		// ③用类加载器加载流资源  [如下,写的路径是项目的相对路径,如果该资源文件不存在,会出现硬编码问题]
		// 资源文件在src目录下面  写路径:类加载器    路径不需要写/
		InputStream is = classLoader.getResourceAsStream("db.properties");
		// 资源文件在包路径下面 写路径:包路径/子包路径/子包路径...../资源文件的名字
		//InputStream is = classLoader.getResourceAsStream("com/zhengqing/Demo/db.properties");
		// ④properties加载流资源
		properties.load(is);
		// ⑤读取数据:getProperty(key):返回资源文件中对应key中的值(字符串),若key不存在则返回null
		System.out.println(properties.getProperty("username"));// root
		System.out.println(properties.getProperty("password"));// 1
		System.out.println(properties.getProperty("notexits"));// null
	}

}

ex3:类加载器获取流  ==》通过线程获得类加载器 

public class TestProperties {

	public static void main(String[] args) throws IOException {
		testReadProperties4();
	}
	// 第三种方式:类加载器获取流       ==>通过线程获得类加载器  
	public static void testReadProperties4() throws IOException {
		// ①创建Properties对象:用于读取文件
		Properties properties = new Properties();
		// ②通过线程获得类加载器
		ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
		// ③类加载器获得获得流资源
		// 资源文件在src目录下面:直接写文件路径   不需要/        如果写在其他的包路径==》包路径/子包路径/子包路径...../资源文件的名字
		InputStream is = contextClassLoader.getResourceAsStream("db.properties");
		//InputStream is = contextClassLoader.getResourceAsStream("com/zhengqing/Demo/db.properties");
		// ④properties加载流资源
		properties.load(is);
		// ⑤读取数据:getProperty(key):返回资源文件中对应key中的值(字符串),若key不存在则返回null
		System.out.println(properties.getProperty("username"));// root
		System.out.println(properties.getProperty("password"));// 1
		System.out.println(properties.getProperty("notexits"));// null
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郑清

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值