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 ){} |
File. getCanonicalPath () and File. getAbsolutePath () are only different for new File (".") and new File.
# For the getCanonicalPath () function, "." indicates the current folder, and ".." indicates the upper-level folder of the current folder.
# For the getAbsolutePath () function, no matter "." or ".", the current path is returned and the path you set when new File () is added.
# As for the getPath () function, all you get is the path you set in new File ().
For example, the current path is C: test:
The code is as follows: |
Copy code |
File directory = new File ("abc "); Directory. getCanonicalPath (); // C: testabc Directory. getAbsolutePath (); // C: testabc Direcotry. getPath (); // abc is returned. File directory = new File ("."); Directory. getCanonicalPath (); // C: test Directory. getAbsolutePath (); // The C: test. Direcotry. getPath (); // The result is. File directory = new File (".."); Directory. getCanonicalPath (); // C: Directory. getAbsolutePath (); // The C: test .. Direcotry. getPath (); // The result is .. |
Obtains the current working directory of the JAVA program.
File file = new File ("t. tmp ");
String fullpath = file. getAbsolutePath ();
1. request. getRealPath
Method: request. getRealPath ("/")
Path: C: Program FilesApache Software FoundationTomcat 5.5 webjavasstrutstest
Method: request. getRealPath (".")
Path: C: Program FilesApache Software FoundationTomcat 5.5web+strutstest.
Method: request. getRealPath ("")
Path: C: Program FilesApache Software FoundationTomcat 5.5 webjavasstrutstest
Request. getRealPath ("web. xml ")
C: Program FilesApache Software FoundationTomcat 5.5web#strutstestweb.xml
2. request. getParameter ("");
ActionForm. getMyFile ();
Method: String filepath = request. getParameter ("myFile ");
Path: D: vssinstallation directory users.txt
Method: String filepath = ActionForm. getMyFile ();
Path: D: vssinstallation directory users.txt