java反射 jdbc 利用反射读取属性文件

本文介绍了一个Java程序中类加载器的使用及如何通过类加载器读取属性文件。展示了不同类加载器之间的区别,以及如何利用类加载器获取资源流并解析属性文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class test01 {
	private String name;

	public test01(String name) {
		super();
		this.name = name;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		test01 other = (test01) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	public static void main(String[] args) {
		/*
		 * test01 t1=new test01("1"); test01 t2=new test01("");
		 * System.out.println(t1.equals(t2));
		 */
		System.out.println(String.class.getClassLoader());
		System.out.println(test01.class.getClassLoader());
		
		System.out.println(String.class.getClassLoader().getSystemResourceAsStream("test.prop"));
		System.out.println(test01.class.getClassLoader().getSystemResourceAsStream("test.prop"));
	
		InputStream in=test01.class.getClassLoader().getSystemResourceAsStream("test.prop");
		Properties prop=new Properties();
		try {
			prop.load(in);
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
		System.out.println(prop.getProperty("key"));
	
	}
}

输出
null
sun.misc.Launcher$AppClassLoader@73d16e93
java.io.BufferedInputStream@15db9742
java.io.BufferedInputStream@6d06d69c
VAL

注:工程src文件夹下有test.prop文件,内容为:
key=VAL

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值