ClassLoad类加载器读取ClassPath路径下的配置文件(一)

ClassLoad类加载器读取ClassPath路径下的配置文件(一)


一、项目介绍

1、使用 com.test.test包下的ReadclassLoad 类去读取另一个包 com.test.util 包  ClassPath路径下的properties 文件。项目路径如下图:




2、实例代码

package com.test.test;

import java.io.IOException;
import java.io.InputStream;

import org.junit.Test;

public class ReadclassLoad {
	
	//通过类加载器获取 ClassPath 路径下的文件
	@Test
	public void readLoad() throws IOException{
		//1、获取类加载器
		ClassLoader cd = ReadclassLoad.class.getClassLoader();
		//2、用类加载器读取文件信息
		InputStream in = cd.getResourceAsStream("jdbc.properties");
		//3、创建数组,遍历文件信息
		byte[] by = new byte[in.available()];
		int len = in.read(by);
		System.out.println(new String(by,0,len));
	}

}

3、运行结果,读取到配置文件的配置信息


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值