Creation of JAVA file and absolute path to relative path

Source: Internet
Author: User

http://blog.sina.com.cn/s/blog_9386f17b0100w2vv.html JAVA file creation and relative path absolute path(2011-12-09 08:27:56) reproduced
Tags: gossip Category: Technology

File F = new file ("D:/test/mytest.txt"); After executing this sentence there is an F application in the memory stack space, and there is a Mytest.txt object in the heap space. Attention

This object contains only the attributes of the file (such as size, whether it is readable, modified, etc.) and does not contain the contents of the file, so length=0. When we want to perform the operation on the file, this time

When the abstract path works, for example, when we want to execute the f.createnewfile () command, the virtual opportunity translates the abstract path to the actual physical path to the transformed physical path (at this point

is the hard disk) under which the file is created. At this point, if there is no test folder in your D drive, then embarrassed, the program will throw an exception, if there is a test folder, you can create a

Mytest.txt file now. Can not create Mytest.txt is that the file above there is no Test folder, which is the abstract path is loaded strange.
What if I want the F reference to create the folder on the hard disk? Use F.getparentfile () to find all the folders above the Mytest.txt, then the mkdirs () is done.

-----------------------
-----------------------
The file class is the class used to construct files or folders, and in its constructor, it is required to pass in a string parameter that indicates the path where the file resides. The absolute path has been used previously as the parameter

The relative path can also be used here. Using absolute paths Needless to say, it's easy to navigate to a file, so how does the relative path JVM locate the file?

According to the JDK Doc, the absolute pathname is the full pathname, and no additional information is required to locate the file that it represents. Instead, the relative pathname must use a different path from the

The information for the path name is explained. By default, the classes in the Java.io package always parse relative path names based on the current user directory. This directory is specified by the system attribute User.dir, which is typically

The calling directory for the Java virtual machine.

Relative path as the name implies, relative to a certain path, then we must understand what the path is relative to. The path described in the JDK document above is "Current user directory" and "

The calling directory for the Java virtual machine. More clearly, this path is where we call the JVM's path. For example:

Suppose there is a Java source file Example.java in the D packing directory, the file does not contain package information. We go to the command-line window and then use the "D:" command to switch to the D-Packing directory and

Use "Javac Example.java" to compile this file, compile error-free, will be in the D packing directory automatically generated "Example.class" file. We're calling "Java Example" to run

The program. At this point we have started a JVM, which is started in the D-Packing directory, so the relative path of the file class in the program loaded by this JVM is relative to this path, i.e.

D Packing directory: D:\. Also the "Current user directory" is also d:\. In System.getproperty ("User.dir"); the system variable "User.dir" is also the value that is stored.

We can do a few more experiments, move "example.class" to different paths, and under those paths, execute the "Java Example" command to start the JVM, we will find this "current

The user directory is constantly changing, and its path is always the same as where we started the JVM.

Knowing this, we can use relative paths to create files, for example:

File File = new file ("A.txt");

File.createnewfile ();

Assuming that the JVM was started under "D:\", then A.txt would be generated in D:\a.txt;

In addition, this parameter can also use some common path representations, such as "." or ". \" represents the current directory, which is the JVM boot path. So the following code can get the current target

Record the full path:

File F = new file (".");

String Absolutepath = F.getabsolutepath ();

System.out.println (Absolutepath);//d:\

Finally, the situation in eclipse:

The startup JVM in Eclipse is started on the project root path. For example, there is a project named blog, the full path is: D:\work\IDE\workspace\blog. Then this path is the JVM's start

The above code if it is running in Eclipse, the output is "D:\work\IDE\workspace\blog."

The situation in Tomcat.

If you are running a web app in Tomcat, at this point, if we use the following code in a class:

File F = new file (".");

String Absolutepath = F.getabsolutepath ();

System.out.println (Absolutepath);

Then the output will be the bin directory under Tomcat. My machine is "D:\work\server\jakarta-tomcat-5.0.28\bin\.", thus it can be seen that the Tomcat server is in the bin

Record the start of the JVM. In fact, the JVM is started in the "Catalina.bat" file in the bin directory.

Creation of JAVA file and absolute path to relative path

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.