This is the seventh day of my participation in the August More text Challenge. For details, see:August is more challenging

There are two ways to get resources in Java, file system-based and classpath based.

  • File system based

    1. Relative path. Such asFile f = new File("text.txt"), and the file text. TXT is the relative path. The general relative path isSystem.getProperties("user.dir")That is, the path where the Java program is executed. For example, if you use CMD in Windows to execute a Java program by calling a Java command, the current path in CMD is the relative path in the Java program. When Eclipse executes a Java program, the project path is a relative path. Note that the root path is the current drive letter path. Such as:"/text.txt" = "D:/text.txt"
    2. Absolute path. Such asnew File("C:/text.txt")
  • Based on the classpath

    As we know, when Java commands are executed, you can specify a classpath, and the system looks for classes in this classpath by default. Files, JAR packages, configuration files, etc. There are three ways to obtain resources based on the CLASspath:

    URL url = this.getClass().getResource("resource_name"); URL = this.getClass().getClassLoader().getResource("resource_name"); URL = thread.currentThread ().getContextClassLoader().getResource("resource_name"); // (Recommended) The thread's context ClassLoader, a container such as Tomcat, may use a custom ClassLoader to generate a special classpath, so it needs to follow a special way.Copy the code

    Demo: Use Eclipse to create a Java Project named Learning under D:\eclipseLuna64\ myWorkspace

    FilePath.java package my.learning; import java.io.File; import java.io.FileOutputStream; import java.net.URL; Public class FilePath {public static void main(String[] args){system.out.println (" relative path based on file System: " + System.getProperty("user.dir")); System. The out. Println (" based on the current Class of the classpath path: "+ FilePath. Class. The getResource (" ")); System. The out. Println (" based on the current Class of classpath root: "+ FilePath. Class. The getResource ("/")); System. The out. Println (" based on the current this classpath path: "+ FilePath. Class. GetClassLoader (). The getResource (" ")); System.out.println(" classpath path based on thread.currentThread ().getContextClassLoader() : " + Thread.currentThread().getContextClassLoader().getResource("")); }}Copy the code
The output is: relative path based on file system: D:\eclipseLuna64\myworkspace\Learning classpath path based on current Class: File: / D: / eclipseLuna64 / myworkspace/Learning/bin/my/Learning/based on the current Class of the classpath with path: File: / D: / eclipseLuna64 / myworkspace/Learning/bin/based on the current the classpath of this path: File: / D: / eclipseLuna64 / myworkspace/Learning/bin/based on Thread. The currentThread () getContextClassLoader classpath path () : File: / D: / eclipseLuna64 / myworkspace/Learning/bin / * * special note: under the Tomcat web project root is: web - INF \ classes * *Copy the code