反射调用任何对象的方法
// 调用任何对象的方法
public Object invokeAny(Object obj, String metnodName, Class<?>[] type, Object... args) throws Exception {
Class<?> class1 = obj.getClass();
// 获取方法
Method method = class1.getMethod(metnodName, type);
// 调用
return method.invoke(obj, args);
}
@Test
public void test3() throws Exception {
Properties properties = new Properties();
// properties.setProperty("name", "李四");
// System.out.println(properties.toString());
invokeAny(properties, "setProperty", new Class[] { String.class, String.class }, "username", "李四");
System.out.println(properties.toString());
}
Properties
Properties
:Properties类表示一组持久的属性。 Properties可以保存到流中或从流中加载。 属性列表中的每个键及其对应的值都是一个字符串。
getProperty(String key)
:使用此属性列表中指定的键搜索属性。如果在此属性列表中找不到该键,则会默认属性列表及其默认值递归。 如果找不到属性,该方法返回null 。
获取路径
流和反射
package com.company.reflect;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class IoPropertiesTest {
public static void main(String[] args) {
try {
// String path = Thread.currentThread().getContextClassLoader().getResource("userinfo2.properties").getPath(); // 只能获取src下面的
// FileReader r = new FileReader(path);
InputStream r = Thread.currentThread().getContextClassLoader().getResourceAsStream("userinfo2.properties");
Properties p = new Properties();
p.load(r);
String className = p.getProperty("className");
System.out.println(className);
r.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
类加载器
package com.company.reflect;
import java.io.FileNotFoundException;
/**
研究一下文件路径的问题。
怎么获取一个文件的绝对路径。以下讲解的这种方式是通用的。但前提是:文件需要在类路径下。才能用这种方式。
*/
public class AboutPath {
public static void main(String[] args) throws FileNotFoundException {
// 这种方式的路径缺点是:移植性差,在IDEA中默认的当前路径是project的根。
// 这个代码假设离开了IDEA,换到了其它位置,可能当前路径就不是project的根了,这时这个路径就无效了。
// FileReader reader = new FileReader("userinfo3.properties");
// 接下来说一种比较通用的一种路径。即使代码换位置了,这样编写仍然是通用的。
// 注意:使用以下通用方式的前提是:这个文件必须在类路径下。
// 什么类路径下?方式在src下的都是类路径下。【记住它】
// src是类的根路径。
/**
解释:
Thread.currentThread() 当前线程对象
getContextClassLoader() 是线程对象的方法,可以获取到当前线程的类加载器对象。
getResource() 【获取资源】这是类加载器对象的方法,当前线程的类加载器默认从类的根路径下加载资源。
*/
// /E:/IDEA-java/chapter15/out/production/chapter15/userinfo2.properties
String path1 = Thread.currentThread().getContextClassLoader()
.getResource("userinfo2.properties").getPath(); // 这种方式获取文件绝对路径是通用的。
System.out.println(path1);
// 从类的根路径下作为起点开始
// /E:/IDEA-java/chapter15/out/production/chapter15/com/company/reflect/%e9%9d%92%e7%8e%89%e6%a1%88
String path2 = Thread.currentThread().getContextClassLoader().getResource("com/company/reflect/青玉案").getPath();
System.out.println(path2);
}
}
ResourceBundle
package com.company.reflect;
import java.util.ResourceBundle;
/**
java.util包下提供了一个资源绑定器,便于获取属性配置文件中的内容。
使用以下这种方式的时候,属性配置文件xxx.properties必须放到类路径下。
*/
public class ReSourceBundleTest {
public static void main(String[] args) {
// 资源绑定器,只能绑定xxx.properties文件。并且这个文件必须在类路径下。文件扩展名也必须是properties
// 并且在写路径的时候,路径后面的扩展名不能写。
//ResourceBundle bundle = ResourceBundle.getBundle("userinfo2");
ResourceBundle bundle = ResourceBundle.getBundle("com\\company\\reflect\\userinfo3"); // 不要和项目根下的同名
String className = bundle.getString("className");
System.out.println(className);
}
}
properties文件:
className=java.lang.String #自己写