1. What is an absolute path? What is the relative path?

Absolute path: A path pointing to a fixed location in a file system. It does not change with the current working directory. To do this, it must include the root directory.

Example: test. TXT file's absolute path as: D: \ documents \ develop \ test TXT https://www.oracle.com/index.html represents a URL absolute pathCopy the code

Relative path: it is based on the specified working directory, avoiding providing the complete absolute path. The file name can be thought of as a relative path (although it is not generally referred to as a path) specifying the working directory as the base.

What is the relative path to which files are loaded in Java? According to the Java Doc, the relative path Java uses is relative to the current user directory, the Java VIRTUAL machine's calling directory, or more simply the path where we called the JVM. The test. Java file does not contain package information. Compile the file in the command line window (run the following command: Javac test. Java), the test. class file will be automatically generated in drive D. Then execute the program in a command line window (execute command: D:\ 'current user directory' = 'current user directory'; D:\ 'current user directory' = 'current user directory'; D:\ 'current user directory' = 'current user directory'; D:\ System.getProperty("user.dir") system variables"user.dir"Note: Move test. class to a different directory and run Java Test to start the JVM. Find that the current user directory is always the same as where the JVM was started.Copy the code

From Wikipedia: zh.wikipedia.org/wiki/ path_ (calculate…

2. Path classification

3. Path differences and operation apis

3.1 Relative path of Java Class

  • Path relative to classpath

    Classpath: is the path where the. Class files are stored in the project.

    Example: The classpath in the Eclipse project is: D: eclipse32\workspace\java-demo\bin, relative to the classpath path, is relative to D: eclipse32\workspace\java-demo\bin. (the form of a URL is: file: / D: / eclipse32 / workspace/Java - demo/bin /) IntelliJ IDEA project classpath as follows: D:\ideaProjectDemo\java-demo\target\classes, relative to the classpath path, is relative to D:\ideaProjectDemo\java-demo\target\classes. (the form of a URL is: file: / D: / ideaProjectDemo/Java - demo/target/classes /)Copy the code
  • Path relative to the current user directory

    Current user directory: the calling directory of the Java VM, the path returned by System.getProperty(“user.dir”).

    For general projects, this is the root directory of the project, for example, the root directory of the java-demo project is: D:\ideaProjectDemo\java-demo.

    For JavaEE servers, this may be a path to the server. There is no uniform specification for this. For example, if you are running a Web application in Tomcat, the “current user directory” is: D:\Program Files\tomcat-5.0.28\bin, D:\Program Files\tomcat-5.0.28\bin, From this you can see that the Tomcat server started the JVM in the bin directory (actually started the JVM in the “Catalina.bat” file in the bin directory).

    Description:

    By default, classes in the java.io package always analyze relative pathnames based on the “current user directory,” specified by the system property user.dir, which is usually the calling directory of the Java virtual machine.

    That is: when working with classes in the java.io package, it is best not to use relative paths. (In A J2EE application, this path is different on different servers.)

    Therefore, do not use a path relative to the current user directory.

3.2 Web Application relative path

  • Relative address of the server

    Server-side relative address refers to the address relative to your Web application, which is resolved on the server side. That is to say, in the JSP and servlet relative address is relative to your web application, namely relative to http://192.168.0.1/webapp/.

    Example: 1. The servlet: request. GetRequestDispatcher ("/user/index.jsp"), the"/user/index.jsp"Is relative to the current web application webapp directory, the absolute address is: http://192.168.0.1/webapp/user/index.jsp 2. JSPS: < % response. The sendRedirect ("/user/a.jsp"); % > its absolute address is: http://192.168.0.1/webapp/user/a.jspCopy the code
  • Relative address of the client

    All of the relative address in the HTML page is relative to the root directory of the server (http://192.168.0.1/), rather than relative to the Web application server root directory directory (http://192.168.0.1/webapp/).

    For example, the address of the action attribute in the HTML form is relative to the server root (http://192.168.0.1), so the submission to index.jsp is: action="/webapp/user/index.jsp"Or action ="<%=request.getContextPath()%>/user/a.jsp"; Note: In general, it is best to put <%=request.getContextPath()%> in front of the CSS and javascript.action attributes referenced by JSP/HTML pages to ensure that the referenced files belong to the directory in the Web application. Note: Use should be avoided as much as possible".".". /".".. /.. /"And so on relative to the location of the file, otherwise when the file is moved, it is easy to cause problems.". /"Represents the current directory".. /"Represents the parent directory".. /.. /"Indicates the parent directory of the parent directoryCopy the code