Java to get the current path (current folder, current directory) 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 obtain the current path:
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 (iow.tin e ){}
File. GetCanonicalPath () and file. getabsolutepath () are approximatelyNew file (".")AndNew file ("..")The two paths are different.
# For the GetCanonicalPath () function,."Indicates the current folder, and".."Indicates the upper-level folder of the current folder
# For the getabsolutepath () function, no matter".","..", Returns the current path with the path you set when new file ()
# As for the getpath () function, all you get is the path you set in new file ().
For example, the current path is c: \ test:
File directory = new file ("ABC ");
Directory. GetCanonicalPath (); // The result is C: \ test \ ABC
Directory. getabsolutepath (); // The result is C: \ test \ ABC
Direcotry. getpath (); // ABC is returned.
File directory = new file (".");
Directory. GetCanonicalPath (); // The result is C: \ test
Directory. getabsolutepath (); // The result is C: \ test \.
Direcotry. getpath (); // The result is.
File directory = new file ("..");
Directory. GetCanonicalPath (); // The result is C :\
Directory. getabsolutepath (); // The result is C: \ test \..
Direcotry. getpath (); // The result is ..
In addition, the string parameters in system. getproperty () are as follows:
System. getproperty () Parameters
# 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 directory1. Use the system. getproperty () function to obtain the current path:
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:
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 (iow.tin e ){}
File. GetCanonicalPath () and file. getabsolutepath () are approximatelyNew file (".")AndNew file ("..")The two paths are different.
# For the GetCanonicalPath () function,."Indicates the current folder, and".."Indicates the upper-level folder of the current folder
# For the getabsolutepath () function, no matter".","..", Returns the current path with the path you set when new file ()
# As for the getpath () function, all you get is the path you set in new file ().
For example, the current path is c: \ test:
File directory = new file ("ABC ");
Directory. GetCanonicalPath (); // The result is C: \ test \ ABC
Directory. getabsolutepath (); // The result is C: \ test \ ABC
Direcotry. getpath (); // ABC is returned.
File directory = new file (".");
Directory. GetCanonicalPath (); // The result is C: \ test
Directory. getabsolutepath (); // The result is C: \ test \.
Direcotry. getpath (); // The result is.
File directory = new file ("..");
Directory. GetCanonicalPath (); // The result is C :\
Directory. getabsolutepath (); // The result is C: \ test \..
Direcotry. getpath (); // The result is ..
In addition, the string parameters in system. getproperty () are as follows:
System. getproperty () Parameters
# 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 directory