File absolute path acquisition problem

Usually  FileReader reader = new FileReader("classToGet.properties"); Get path problem, but the defect of this way of path: poor portability , leaving idea and changing to another location, this path will be invalid.

1. A more general path acquisition

But there is a premise : this file must be in the classpath (that is , all under the src are classpath)

code show as below:

public class reflect_path {
    public static void main(String[] args) throws FileNotFoundException {
        //这个是在src的bean目录下的
        String path = Thread.currentThread().getContextClassLoader().getResource("cn/com/Ycy/Beans/name.properties").getPath();
        System.out.println(path);
        String path2 = Thread.currentThread().getContextClassLoader().getResource("classToGet.properties").getPath();
        System.out.println(path2);
    }
}

Output result:

/D:/ideaProject/TCP/InetAddress/out/production/InetAddress/cn/com/Ycy/Beans/name.properties
/D:/ideaProject/TCP/InetAddress/out/production/InetAddress/classToGet.properties

In this way, we can write the creation object as follows:

public class test03 {
    public static void main(String[] args) throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException {
        String path = Thread.currentThread().getContextClassLoader().getResource("classToGet.properties").getPath();
        FileReader reader = new FileReader(path);
        //创建属性类对象map
        Properties pro = new Properties();
        //加载
        pro.load(reader);
        //关闭流
        reader.close();
        String className = pro.getProperty("className");
        Class c = Class.forName(className);
        Object user = c.newInstance();
        System.out.println(user);
    }
}

2. Return directly in the form of a stream

It can be in the form of simplified code, instead of obtaining the path to obtain the xxx.properties file, and directly returning to the stream form, we don’t need to use FileReader anymore.

public class IO_return {
    public static void main(String[] args) throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException {
        InputStream reader = Thread.currentThread().getContextClassLoader().getResourceAsStream("classToGet.properties");
        //创建属性类对象map
        Properties properties = new Properties();
        //加载
        properties.load(reader);
        reader.close();
        Class c = Class.forName(properties.getProperty("className"));
        Object obj = c.newInstance();
        System.out.println(obj);
    }
}

3. Resource binder (the easiest way)

Yes, it's snowy in front, hahaha, I feel cheated, it's so useful.

The java.util.ResourceBundle
       class is specifically used to obtain the content of the property configuration file .

Only the "xxx.properties" property configuration file can be read, and the file must be placed in the src class path .

Note: This class can only read the "xxx.properties" property configuration file, and the file must be placed in the src class path.

Code demo:

public class ResourceBundleTest {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        //这个name.properties是在Beans目录下的
        ResourceBundle bundle = ResourceBundle.getBundle("cn/com/Ycy/Beans/name");
        String className = bundle.getString("className");
        Class c  = Class.forName(className);
        Object obj = c.newInstance();
        System.out.println(obj);


        ResourceBundle bundle2 = ResourceBundle.getBundle("classToGet");
        String className2 = bundle2.getString("className");
        Class c2  = Class.forName(className2);
        Object obj2 = c2.newInstance();
        System.out.println(obj2);
    }
}

Guess you like

Origin blog.csdn.net/weixin_43725517/article/details/108300774