API for Action path in file (GO)

Source: Internet
Author: User
Tags file separator

These days have been engaged in Java, Template engine series and the program Ape Practice series are suspended, busy in Java the exhilaration! Due to the lack of familiarity with Java, through a variety of entanglements and finally completed the task. The following is a memo about how Java gets the current directory.

Original address: http://www.cnblogs.com/diyunpeng/archive/2011/06/06/2073567.html

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 only aboutNew File (".")AndNew File ("..")The two paths differ.

# for the Getcanonicalpath () function, "."Represents the current folder, and the".."Represents the top level folder in the current folder
# for the GetAbsolutePath () function, regardless of the ".”、“..", return 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 to beC:\TEST\ABC
Directory.getabsolutepath ();Got to beC:\TEST\ABC
Direcotry.getpath (); Got ABC.

file directory = new file (".");
Directory.getcanonicalpath ();Got to beC:\test
Directory.getabsolutepath ();Got to beC:\test\.
Direcotry.getpath ();///Get it.

file directory = new file ("..");
Directory.getcanonicalpath ();Got to beC +
Directory.getabsolutepath ();Got to beC:\test\.
Direcotry.getpath ();///Get a.




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) Span class= "Apple-converted-space" > 
# 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

http://www.cnblogs.com/ymind/archive/2012/04/22/2465629.html

java get current file path:

The first type:
File F = new file (This.getclass (). GetResource ("/"). GetPath ());
System.out.println (f);
Results:
C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin
Gets the project path where the current class is located;
If you do not add "/"
File F = new file (This.getclass (). GetResource (""). GetPath ());
System.out.println (f);
Results:
C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin\com\test
Gets the absolute path of the current class;

The second type:
File directory = new file ("");//parameter is empty
String coursefile = Directory.getcanonicalpath ();
System.out.println (Coursefile);
Results:
C:\Documents and Settings\administrator\workspace\projectname
Gets the project path where the current class is located;

The third type:
URL Xmlpath = This.getclass (). getClassLoader (). GetResource ("Selected.txt");
System.out.println (Xmlpath);
Results:
File:/c:/documents%20and%20settings/administrator/workspace/projectname/bin/selected.txt
Gets the path to the Selected.txt file in the current project SRC directory

The fourth type:
System.out.println (System.getproperty ("User.dir"));
Results:
C:\Documents and Settings\administrator\workspace\projectname
Get current project path

The fifth type:
System.out.println (System.getproperty ("Java.class.path"));
Results:
C:\Documents and Settings\administrator\workspace\projectname\bin
Get current project path

Http://www.cnblogs.com/lostyue/archive/2011/06/27/2091686.html

API for Action path in file (GO)

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.