Java IO Tutorial

December 14, 2020 No comments javaio java tutorial
Java IO Tutorial

Java I/O is an API in Java dedicated to reading data from various inputs and writing to the outputs. Reading data, processing, and generating output is a common use-case in many applications making this functionality really useful. Java IO classes are located in java.io package.

Java File Class

Java java.io.File is an abstract representation of file and directory pathnames. The class is used for filesystem operations (like create, delete, rename, search) on files and directories. File instance is a handler to file or directory on the filesystem. In the below list we present common methods available on File class:

Method Description
public String getName() Returns the name of the file or directory.
public String getParent() Returns the name of the parent directory for specified File.
public File getParentFile() Returns the File representation of parent directory if given file have a parent.
public String getPath() Returns string representation of the file path.
public boolean isAbsolute() Checks whether given pathname is absolute. Returns true if this abstract pathname is absolute, false otherwise.
public String getAbsolutePath() Returns the absolute path of the given file or directory.
public boolean canRead() Tests if the application can read the specified file.
public boolean canWrite() Checks whether the application can modify to the file denoted by this abstract pathname.
public boolean canExecute() Tests whether the application can execute the file.
public boolean exists() Tests whether the file or directory with a specified pathname exists in the filesystem.
public boolean isDirectory() Tests whether the file denoted by this abstract pathname is a directory.
public boolean isFile() Tests whether the file denoted by this abstract pathname is a normal file.
public long lastModified() Returns the time that the file was last modified.
public long length() Returns the length of the file denoted by this abstract pathname.
public boolean createNewFile() Creates a new and empty file with specified name.
public boolean delete() Deletes the file or directory but assuming the directory is empty.
public void deleteOnExit() Qualifies the specified file or directory for deletion when the virtual machine terminates.
public String[] list() Returns an array of strings naming the files and directories in the given directory.
public String[] list(FilenameFilter filter) Returns an array of strings naming the files and directories (in the given directory) that satisfy the specified filter.
public File[] listFiles() Returns an array of the files in the directory.
public File[] listFiles(FileFilter filter) Returns an array of files in the directory that satisfy the specified filter.
public File[] listFiles(FilenameFilter filter) Returns an array of files in the directory that satisfy the specified filter.
public boolean mkdir() Creates the directory.
public boolean mkdirs() Creates the directory, including any necessary but nonexistent parent directories.
public boolean renameTo(File dest) Renames the file.
public boolean setLastModified(long time) Sets the last-modified time of the file or directory.
public boolean setReadOnly() Marks the file or directory so that only read operations are allowed.
public static File createTempFile(String prefix, String suffix, File directory) Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name.
public static File createTempFile(String prefix, String suffix) Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name.
public int compareTo(File pathname) Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name.
public boolean equals(Object obj) Tests if the given pathname is equal to that provided in the argument.
public String toString() Returns the pathname string of the given File object.
public long getFreeSpace() Returns the number of unallocated bytes in the partition named by given path.
public String getCanonicalPath() Returns the canonical path of the given file or directory.
public File getCanonicalFile() Returns the canonical form of the file or directory.
{{ message }}

{{ 'Comments are closed.' | trans }}