Working with the Files

The java.io package contains a class known as the File class that provides support for creating files and directories. This class includes several constructors for instantiating the File objects. This class also contains several methods for supporting the operations such as :—

  • Creating a file
  • Opening a file
  • Closing a file
  • Deleting a file
  • Renaming a file
  • Getting the name of a file
  • Getting the size of a file
  • Checking the existence of a file
  • Checking whether the file is readable
  • Checking whether the file is writable
 
File class signature:
public class File extends Object implements Serializable, Comparable

 

The File class deals with the machine dependent files in a machine-independent manner i.e. it is easier to write platform-independent code that examines and manipulates files using the File class. The java.io.File is the central class that works with files and directories. The instance of this class represents the name of a file or directory on the host file system. Desciptions about the fields, constructors and methods of this class are provided here.

 

Field description is given below: 

Field Summary

static String pathSeparator

The system-dependent path-separator character, represented as a string for convenience.

static char pathSeparatorChar

The system-dependent path-separator character.

static String separator

The system-dependent default name-separator character, represented as a string for convenience.

static char separatorChar

The system-dependent default name-separator character.

 

 

Constructor description is given below: 

Constructor Summary

File(File parent, String child)
          

Creates a new File instance from a parent abstract pathname and a child pathname string.

File(String pathname)

Creates a new File instance by converting the given pathname string into an abstract pathname.

File(String parent, String child)

Creates a new File instance from a parent pathname string and a child pathname string.

File(URI uri)
          

Creates a new File instance by converting the given file: URI into an abstract pathname.

 

 

Method description is given below: 

Method Summary

boolean canRead()

Tests whether the application can read the file denoted by this abstract pathname.

boolean canWrite()   

Tests whether the application can modify the file denoted by this abstract pathname.

int compareTo(File pathname)

Compares two abstract pathnames lexicographically.

boolean createNewFile()

Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.

static File createTempFile(String  prefix, String suffix)

Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name.

static File createTempFile (String prefix, String suffix, File directory)

Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name.

boolean delete()

Deletes the file or directory denoted by this abstract pathname.

void deleteOnExit()

Requests that the file or directory denoted by this abstract pathname be deleted when the virtual machine terminates.

boolean equals(Object obj)

Tests this abstract pathname for equality with the given object.

boolean exists()

Tests whether the file or directory denoted by this abstract pathname exists.

File getAbsoluteFile()

Returns the absolute form of this abstract pathname.

String getAbsolutePath()

Returns the absolute pathname string of this abstract pathname.

File getCanonicalFile()

Returns the canonical form of this abstract pathname.

String getCanonicalPath()

Returns the canonical pathname string of this abstract pathname.

String getName()

Returns the name of the file or directory denoted by this abstract pathname.

String getParent()

Returns the pathname string of this abstract pathname’s parent, or null if this pathname does not name a parent directory.

File getParentFile()

Returns the abstract pathname of this abstract pathname’s parent, or null if this pathname does not name a parent directory.

String getPath()

Converts this abstract pathname into a pathname string.

int hashCode()

Computes a hash code for this abstract pathname.

boolean isAbsolute()

Tests whether this abstract pathname is absolute.

boolean isDirectory()

Tests whether the file denoted by this abstract pathname is a directory.

boolean isFile()

Tests whether the file denoted by this abstract pathname is a normal file.

boolean isHidden()

Tests whether the file named by this abstract pathname is a hidden file.

long lastModified()

Returns the time that the file denoted by this abstract pathname was last modified.

long length()

Returns the length of the file denoted by this abstract pathname.

String[]  list()

Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname.

String[] list(FilenameFilter filter)

Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.

File[]  listFiles()

Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname.

File[]  listFiles(FileFilter filter)

Returns an array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.

File[]  listFiles(FilenameFilter filter)

Returns an array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.

static File[]  listRoots()

List the available filesystem roots.

boolean mkdir()

Creates the directory named by this abstract pathname.

boolean mkdirs()

Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories.

boolean renameTo(File dest)

Renames the file denoted by this abstract pathname.

boolean setLastModified(long time)

Sets the last-modified time of the file or directory named by this abstract pathname.

boolean setReadOnly()

Marks the file or directory named by this abstract pathname so that only read operations are allowed.

String toString()

Returns the pathname string of this abstract pathname.

URI toURI()

Constructs a file: URI that represents this abstract pathname.

URL toURL()

Converts this abstract pathname into a file: URL.

 

 

How Files and Streams Work

Java uses streams to handle I/O operations through which the data is flowed from one location to another. For example, an InputStream can flow the data from a disk file to the internal memory and an OutputStream can flow the data from the internal memory to a disk file. The disk-file may be a text file or a binary file. When we work with a text file, we use a character stream where one character is treated as per byte on disk. When we work with a binary file, we use a binary stream (i.e. byte stream). The working process of the I/O streams is shown below:

 

Figure 10: The working process of the I/O streams

 

Things to remember:

Whether the source or destination is a file or a socket, any read or write is performed in three simple steps:

  1. Open the stream.
  2. Until there is more data, keep reading in a read, or writing in a write.
  3. Close the stream.

 

 

In the next section, we will write programs with Java Files and I/O Classes.