getName()
, getAbsoluteFile()
and getCanonicalFile()
getPath()
, getAbsolutePath()
and getCanonicalPath()
Concise version:
File.getName()
returns the file name part as a string; i.e. the bit after the last file separator.File.getPath()
returns the complete pathname as a string.File.getAbsolutePath()
turns the complete pathname as a string, after mapping the path to an absolute path if it is currently relative. No attempt is made to validate the path.File.getAbsoluteFile()
does the same thing as File.getAbsolutePath()
, except that the result is a File
.File.getCanonicalPath()
maps the path to an absolute path (if it is currently relative) and then attempts to canonicalize it. This process is OS dependent, but it typically involves following symbolic links and replacing ".", ".." and empty names with their canonical equivalents. The result is the canonicalized path string.File.getCanonicalFile()
does the same as File.getCanonicalPath()
except that its result is a File
.The first 4 are really just text-based manipulations of the original File
object. They make no attempt to check that any part of the path corresponds to anything in the file system.
The last 2 involve checking the path to the last-named component of the File
. If the path involves non-existent directories, broken links, directories than cannot be read and so on, you are liable to get an IOException.
For more details, follow to the respective methods' javadoc links.
NOTE in 2020 - If you still are cutting new code that uses the File
API, you should seriously consider using the java.nio.file
APIs instead:
Path
object most directly corresponds to a File
objectFiles
and Paths
classes provide numerous static methods that operate on Path
objects,The advantages of the newer APIs include:
File
on Windows (which could not be fixed in File
for compatibility reasons) have been addressed.The Java API has the full description of the File object as well as all methods it contains.
getName(): http://docs.oracle.com/javase/7/docs/api/java/io/File.html#getName()
getCanoniacalPath(): http://docs.oracle.com/javase/7/docs/api/java/io/File.html#getCanonicalPath()
getAbsolutePath(): http://docs.oracle.com/javase/7/docs/api/java/io/File.html#getAbsolutePath()
getAbsoluteFile(): http://docs.oracle.com/javase/7/docs/api/java/io/File.html#getAbsoluteFile()
getCanonicalFile(): http://docs.oracle.com/javase/7/docs/api/java/io/File.html#getCanonicalFile()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With