We have been using the absolute path in the file to create this file class object. But today we found a problem when we used the relative path in the file class, I don't know who the relative path is. My program is like this:
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:
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 turns out that the file was created in the same directory as. classpath, and now the problem with the relative path is known.
The more detailed explanation of the relative path and absolute path is in
Http://www.blogjava.net/zhangchao/archive/2009/04/15/265644.html