"Go" Java to get the current path of several methods

Source: Internet
Author: User
Tags file separator

1. Use the System.getproperty () function to get the current path:
System.out.println (System.getproperty ("User.dir"));//user.dir Specifies the current path

2. Use the function provided by file to get the current path:
File directory = new file ("");//Set as current folder
try{
System.out.println (Directory.getcanonicalpath ());//Get the standard path
System.out.println (Directory.getabsolutepath ());//Get absolute path
}catch (Exceptin e) {}

File.getcanonicalpath () and File.getabsolutepath () are approximately just for the new File (".") and new File ("..") The two paths differ.

# for the Getcanonicalpath () function, "." Represents the current folder, while the ".." Represents the top-level folder in the current folder
# for the GetAbsolutePath () function, regardless of ".", "...", returns the current path plus the path you set at New File ()
# As for the GetPath () function, you get only the path you set at the new File ()

For example, the current path is c:/test:
File directory = new file ("abc");
Directory.getcanonicalpath (); Got the C:/TEST/ABC.
Directory.getabsolutepath (); Got the C:/TEST/ABC.
Direcotry.getpath (); Got ABC.

File directory = new file (".");
Directory.getcanonicalpath (); Got the c:/test.
Directory.getabsolutepath (); Get the c:/test/.
Direcotry.getpath (); Got to be.

File directory = new file ("..");
Directory.getcanonicalpath (); Got the c:/.
Directory.getabsolutepath (); Get the c:/test/.
Direcotry.getpath (); What you get is that.


In addition: the string parameters in System.getproperty () are as follows:
System.getproperty () parameter Daquan
# java.version Java Runtime Environment version
# Java.vendor Java Runtime Environment Vendor
# java.vendor.url Java Vendor URL
# java.home Java installation directory
# java.vm.specification.version Java Virtual Machine specification version
# Java.vm.specification.vendor Java Virtual Machine specification vendor
# Java.vm.specification.name Java Virtual Machine specification name
# java.vm.version Java Virtual Machine implementation version
# Java.vm.vendor Java Virtual Machine Implementation vendor
# Java.vm.name Java Virtual Machine implementation name
# java.specification.version Java Runtime Environment specification version
# Java.specification.vendor Java Runtime Environment specification vendor
# java.specification.name Java Runtime Environment Specification name
# java.class.version Java class format version number
# Java.class.path Java class path
# Java.library.path List of paths to search when loading libraries
# java.io.tmpdir Default Temp file path
# Java.compiler Name of JIT compiler to use
# java.ext.dirs Path of extension directory or directories
# Os.name Operating system name
# Os.arch Operating System Architecture
# os.version Operating System version
# file.separator File Separator ("/" on UNIX)
# Path.separator Path Separator (":" On UNIX)
# Line.separator Line Separator ("/n" on UNIX)
# User.Name user ' s account name
# user.home user ' s home directory
# User.dir User's current working Directoryjava gets the path keyword: Gets the path in Java in Java:


Get path in 1.jsp:

Take the project name test as an example:

(1) Get the full path of the current page containing the project name: Request.getrequesturi ()
Results:/test/test.jsp
(2) Get project name: Request.getcontextpath ()
Results:/test
(3) Get the current page in the same directory under the full name: Request.getservletpath ()
Result: If the page is/test/jsp/test.jsp in the JSP directory
(4) Get the full path of the server on which the page is located: Application.getrealpath ("page. JSP")
Results: d:/resin/webapps/test/test.jsp
(5) Obtain the absolute path of the server on which the page is located: Abspath=new java.io.File (Application.getrealpath (Request.getrequesturi ())). GetParent ();
Results: D:/resin/webapps/test

2. Get the path in the class:

(1) The absolute path of the class: Class.class.getClass (). GetResource ("/"). GetPath ()
Results:/d:/test/webroot/web-inf/classes/pack/
(2) The path to get the project: System.getproperty ("User.dir")
Results: D:/test

3. Get the path in the servlet:

(1) Get the Project catalog: Request.getsession (). Getservletcontext (). Getrealpath ("") parameter can be specific to the package name.
Results: E:/tomcat/webapps/test
(2) Get IE Address bar address: Request.getrequesturl ()
Results: Http://localhost:8080/TEST/test
(3) Get relative Address: Request.getrequesturi ()
Results:/test/test []

"Go" Java to get the current path of several methods

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.