Current path
1. Use the System. getProperty () function to obtain the current path:
| The code is as follows: |
Copy code |
|
System. out. println (System. getProperty ("user. dir"); // user. dir specifies the current path. |
2. Use the function provided by File to obtain the current path:
| The code is as follows: |
Copy code |
|
File directory = new File (""); // Set it to the current folder
Try {
System. out. println (directory. getCanonicalPath (); // Obtain the standard path.
System. out. println (directory. getAbsolutePath (); // Obtain the absolute path.
} Catch (shorttin e ){} |
Absolute path
The following methods are generally used to obtain absolute paths in JAVA:
| The code is as follows: |
Copy code |
|
Request. getRealPath () // This method has expired and is not recommended.
This. getServletContext (). getRealPath () // used in SERVLET
This. getServlet (). getServletContext (). getRealPath () // used in Struts
Request. getSession (). getServletContext (). getRealPath () |
The obtained is the absolute URI path of the current classpath.
| The code is as follows: |
Copy code |
|
Test. class. getResource ("/") |
Obtain the true file path
| The code is as follows: |
Copy code |
|
String file_real_path = ServletContext. getRealPath ("mypath/filename ");?
Request. getRealPath ("/") is not recommended ("/"); |