Java File class
Sujit Kumar
Zenolocity LLC
Copyright @ 2012 - 2024
File Paths
• java.io.File class for representing and
manipulating file paths and do file system
operations.
• File object is immutable. Cannot modify what
it represents after it is created.
• Important: File class is NOT used to read from
a file or write to a file on disk.
• File object can be created with an absolute or
a relative path name.
Important Methods in File Class
• File System Query methods:
canRead(), canWrite(), exists(), isDirectory(),
isFile(), isHidden(), getAbsolutePath(),
lastModified(), length(), listFiles(), listRoots()
• File System Modification methods:
createNewFile(), mkdir(), renameTo(), delete(),
deleteOnExit(), setReadOnly(), setLastModified()
Types of Paths
• Relative, Absolute and Canonical
• getPath(), getAbsolutePath() and getCanonicalPath()
• getPath() is the path that the File object was
constructed with & it may or may not be a relative
path.
• getAbsolutePath() gets the path string after resolving it
against the current directory if it's relative, resulting in
a fully qualified path.
• getCanonicalPath() gets the path string after resolving
any relative path against current directory and removes
any relative paths (. and ..), and any file system
symbolic links to return an absolute path.
Types of Paths (contd…)
• getAbsolutePath() gives you an absolute path
to the file
• getCanonicalPath() gives you the unique
absolute path to the file. Note that there are
more than one absolute paths that point to
the same file, but only one canonical path.
File Separator
• Platform specific directory separator delimiter.
• System property called "file.separator“.
• This character separates components of a file
path. This is "/" on UNIX and "" on Windows.
Examples:
/usr/home/kumar/foo.txt on Unix
C:usrhomekumarfoo.txt on Windows

Java file paths

  • 1.
    Java File class SujitKumar Zenolocity LLC Copyright @ 2012 - 2024
  • 2.
    File Paths • java.io.Fileclass for representing and manipulating file paths and do file system operations. • File object is immutable. Cannot modify what it represents after it is created. • Important: File class is NOT used to read from a file or write to a file on disk. • File object can be created with an absolute or a relative path name.
  • 3.
    Important Methods inFile Class • File System Query methods: canRead(), canWrite(), exists(), isDirectory(), isFile(), isHidden(), getAbsolutePath(), lastModified(), length(), listFiles(), listRoots() • File System Modification methods: createNewFile(), mkdir(), renameTo(), delete(), deleteOnExit(), setReadOnly(), setLastModified()
  • 4.
    Types of Paths •Relative, Absolute and Canonical • getPath(), getAbsolutePath() and getCanonicalPath() • getPath() is the path that the File object was constructed with & it may or may not be a relative path. • getAbsolutePath() gets the path string after resolving it against the current directory if it's relative, resulting in a fully qualified path. • getCanonicalPath() gets the path string after resolving any relative path against current directory and removes any relative paths (. and ..), and any file system symbolic links to return an absolute path.
  • 5.
    Types of Paths(contd…) • getAbsolutePath() gives you an absolute path to the file • getCanonicalPath() gives you the unique absolute path to the file. Note that there are more than one absolute paths that point to the same file, but only one canonical path.
  • 6.
    File Separator • Platformspecific directory separator delimiter. • System property called "file.separator“. • This character separates components of a file path. This is "/" on UNIX and "" on Windows. Examples: /usr/home/kumar/foo.txt on Unix C:usrhomekumarfoo.txt on Windows