Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between the following in an easy and understandable way? (Java Files) [closed]

Tags:

java

file

io

  1. getName(), getAbsoluteFile() and getCanonicalFile()

  2. getPath(), getAbsolutePath() and getCanonicalPath()

like image 630
htnamas Avatar asked Dec 06 '22 11:12

htnamas


2 Answers

Concise version:

  1. File.getName() returns the file name part as a string; i.e. the bit after the last file separator.
  2. File.getPath() returns the complete pathname as a string.
  3. 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.
  4. File.getAbsoluteFile() does the same thing as File.getAbsolutePath(), except that the result is a File.
  5. 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.
  6. 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:

  • a Path object most directly corresponds to a File object
  • the Files and Paths classes provide numerous static methods that operate on Path objects,

The advantages of the newer APIs include:

  • Many operations now throw an exception to tell you why they failed.
  • There is support for a variety of file system semantics; e.g. different kinds of access control, file attributes and so on.
  • There is support for directory watchers, file tree traversal, and so on.
  • There is support for virtual file systems implemented within the JVM.
  • Some of the odd behaviors of File on Windows (which could not be fixed in File for compatibility reasons) have been addressed.
like image 187
Stephen C Avatar answered May 01 '23 12:05

Stephen C


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()

like image 24
calderonmluis Avatar answered May 01 '23 12:05

calderonmluis