If you create a file
using new File("filename")
which is the relative path
, you cannot get the absolute path of the file using file.getAbsolutePath()
, because the relative path is build according to the default user home directory or the JVM path.
Take a look at Java Doc: -
A pathname, whether abstract or in string form, may be either absolute
or relative. An absolute pathname is complete in that no other
information is required in order to locate the file that it denotes.
A relative pathname, in contrast, must be interpreted in terms of
information taken from some other pathname. By default the classes in
the java.io package always resolve relative pathnames against the
current user directory. This directory is named by the system property
user.dir, and is typically the directory in which the Java virtual
machine was invoked.
So, to get the absolute path
for this case, you would actually have to write the path yourself. Get the absolute path
till the directory where you saved the file
, and append the file name
to it.