Absolute path and relative path in java. io. File.

Source: Internet
Author: User
Tags tomcat tomcat server
According to the jdk Doc, "absolute path name is the complete path name. You can locate the file you represent without any other information. On the contrary, the relative path name must be interpreted using information from other path names. By default, classes in the java. io package always analyze relative path names based on the current user directory. This directory is specified by the system attribute "user. dir", which is usually the call directory of the Java virtual machine ."
Relative paths, as the name implies, must be understood as relative to a specific path. according to the jdk Documentation above, the path is the "current user directory", that is, the "Java virtual machine call Directory ". more clearly, this path is actually where we call the jvm path (instead of the jvm directory, the jvm is called in the specified directory ).
For example:
Suppose there is a java source file Example. java is in the root directory of drive D. This file does not contain package information. in the command line window, run the "d:" command to switch to the root directory of drive d, and then use "javac Example. java "to compile this file. After compilation is successful, it will automatically generate" Example. class file. we are calling "java Example" to run the program. now we have started a jvm. This jvm is started under the root directory of disk D. Therefore, the relative path of the File class in the program loaded by this jvm is relative to this path, D :". at the same time, the current user directory "is also D :. in System. getProperty ("user. dir); system variable "user. dir "stores this value.
We can do a few more experiments to put "Example. "class" is moved to different paths. At the same time, execute the "java Example" command to start jvm. We will find that the "current user directory" is constantly changing, its path is always the same as where we started jvm.
1 package hello;
2 public class Test {
3 public static void main (String [] args ){
4 System. out. println ("The current user directory is:" + System. getProperty ("user. dir "));
5}
6}
Copy code
Here I put it in drive D to compile and run
The following directory is available in disk D:
You can see that if you add a package, you can get the current directory of the package where the target class is located (D:/hello), rather than the directory where the target class is located (D:/hello). Pay attention to the differences here.
If you move the entire package (D:/hello) to another disk, such as E:, re-compile the package and the following results are available:
After understanding this, we can use a relative path to create a file, for example:
The code is as follows: Copy code
File file = new file(a.txt ");
File. createNewFile ();
Assume that the jvm is started under "D: Example", then a.txt will be generated in D: a.txt;
In addition, this parameter can also use common path representation methods, such ". "or ". "indicates the current directory, which is the jvm startup path. so the following code can get the complete path of the current directory:
The code is as follows: Copy code
File f = new File (".");
String absolutePath = f. getAbsolutePath ();
System. out. println (absolutePath); // D:
Finally, let's talk about the situation in eclipse:
Jvm is started in Eclipse on the project root path. for example, a project named blog has the complete path D: workIDEworkspaceblog. this path is the jvm startup path. therefore, if the above code is run in eclipse, the output result is "D: workIDEworkspaceblog. "But when creating a File, for example: file File = new file(a.txt"); found that it was created in... blogsrc directory, as shown in the following figure:
In this way, the generation of the target file is stored in the src Directory (. java) instead of the bin directory (. class). This may be related to the eclipse configuration file.
This question is worth your advice ....
 
The following are related articles found on the Internet:

Http://www.111cn.net

"We have been using the absolute path in the File to create this File Class object. However, we found a problem when we used the relative path in the File class today, I don't know who the relative path is. My program is like this:

The code is as follows: Copy code
Package com. csmz. niit;
Import java. io .*;
Public class FileDemo {
Public static void main (String [] args) throws IOException {
Try {
File file = new File ("test.txt ");
System. out. println ("whether the file exists:" + file. exists ());
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
I tried to copy test.txt in src and put FileDemo. java in the same directory. The result still shows false. Later, I tried to copy test.txt to the. class file generated by bin, but the result still failed.
So I came up with a method:
The code is as follows: Copy code
File file = new File ("a.txt ");
File. createNewFile ();
 
I create a text file to see where it is created. The result is run to the source file and it is found that the file is created in the same directory as. classpath, and now the problem with the relative path is known"
 
Tomcat:
If you run a web application in tomcat, use the following code in a class:
The code is as follows: Copy code
File f = new File (".");
String absolutePath = f. getAbsolutePath ();
System. out. println (absolutePath );
The output will be the bin directory under tomcat. my machine is "D: workserverjakarta-tomcat-5.0.28bin. ", we can see that the tomcat server starts jvm in the bin directory. it is actually in the "catalina. bat "file to start the jvm.

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.