Class.getResource() and class getClassLoader(). Getresource() parsing

Posted by HardlyWorking on Thu, 16 Dec 2021 13:54:55 +0100

We wrote it on eclipse The java file will eventually be compiled into a Class bytecode file. The Java source file is stored in the src file The class bytecode file is stored in the bin file.

I Pass class The getresource method returns the URL to get the file or directory

public static void test1() throws Exception, FileNotFoundException{
		//Name of this class class.getResource()
		//Parameter without any path file
		URL url=JDBCConnectin.class.getResource("");
		//Parameter with a "/"
		URL url2=JDBCConnectin.class.getResource("/");
		System.out.println(url); //file:/D:/java/javalianxi/javaproject/bin/yuan/JDBC/
		System.out.println(url2); // file:/D:/java/javalianxi/javaproject/bin/
	}

You can see: JDBC connect class. getResource(""); The URL under the package directory of this class is printed

file:/D:/java/javalianxi/javaproject/bin/yuan/JDBC/

And JDBC connect class. getResource("/"); The URL under the bytecode class root directory is printed

file:/D:/java/javalianxi/javaproject/bin/

The difference between the two is self-evident. The next access is through the file name

public static void test1() throws Exception, FileNotFoundException{
		//Name of this class class.getResource()
		//The parameter without /, represents the package directory where the class is located; In this way, you can directly access the files of the same package as this class
		URL url=JDBCConnectin.class.getResource("123");
		//The parameter with a "/" represents the root directory (bin directory); You need to find the specific location of the file under the root directory to access it
		URL url2=JDBCConnectin.class.getResource("/yuan/JDBC/123");
		System.out.println(url);  //file:/D:/java/javalianxi/javaproject/bin/yuan/JDBC/123
		System.out.println(url2);//file:/D:/java/javalianxi/javaproject/bin/yuan/JDBC/123
	}

It can be seen that the parameter without /, indicates the package directory of the class; In this way, you can directly access the files of the same package as this class

The parameter with a "/" indicates the root directory (bin directory); You need to find the specific location of the file under the root directory to access it

 2. Through classloader Getresource method gets the file URL

In fact, there is an additional getclassloader (), such as JDBC connect class. getClassLoader(). getResource

ClassLoader. The getresource method parameter also has a directory parameter of String type, but classloader The getresource method parameter does not support the parameter '/' because the instance method returns the root directory path where the class is located. If you enter a String starting with "/", null will be returned. If you enter a String without "/", the root directory (bin directory) of the class bytecode will be returned, which can also be similarly understood as src directory

public static void test2() throws Exception, FileNotFoundException{
		ClassLoader cl=JDBCConnectin.class.getClassLoader();
		URL url=cl.getResource("");//Get the class root directory file:/D:/java/javalianxi/javaproject/bin/
		String path=url.getPath();
		System.out.println(path);//D:/java/javalianxi/javaproject/bin/
		
		URL url2=cl.getResource("/");
		System.out.println(url2);  //null
	}

It can be seen that what can be obtained without "/" is under the class root directory, and null will be printed with "/"

public static void test() throws Exception, FileNotFoundException{
		ClassLoader cl=JDBCConnectin.class.getClassLoader();
		URL url=cl.getResource("yuan/JDBC/123");//Get the url of the class root directory: file:/D:/java/javalianxi/javaproject/bin /
		String path=url.getPath();
		BufferedReader bf=new BufferedReader(new InputStreamReader(new FileInputStream(path),"utf-8"));
		int len=0;
		char[] bytes=new char[1024];
		while((len=bf.read(bytes))!=-1){
			System.out.println(new String(bytes,0,len));
		}
		System.out.println(url);
		System.out.println(url.toString());
	}

If the parameter of cl.getResource("yuan/usb/123") is changed to "/ yuan/usb/45", null will be returned

Last: class Getresource and classloader The difference between getresource and getresource is the different loading methods when loading resource files,

class.getResource("/") == class.getClassLoader().getResource("")
Actually, class Getresource and classloader Getresource is essentially the same, using classloader Getresource loads the of the resource. Class.getResource really calls classloader Before the getresource method, the path of the file will be obtained first (when the path does not start with '/', the default is to get the resources from the package where this class is located; when the path starts with '/', the resources are obtained from the ClassPath root of the project).
 

Topics: Java Eclipse