The File Class

When working with data files in Java, the File class from the package is utilized (where IO stands for Input/Output).

Due to certain limitations when working with this package, Java introduced in 2002 the java.nio.file package (where NIO stands for "New" Input Output) to improve file access. When Java 7 came on the scene, additional new features of NIO (now referred to as NIO.2) were introduced. While these new packages can improve the performance and speed of working with files, the coding involved with these new packages can become quite involved.

Fortunately, the basic I/O operations from the package remain adequate for most Java programmers and are adequate for this course.

The File class, which is part of the package, is used to create an object that represents a file. The file object can, among other options, be used to create a new file, see if a file exists, delete a file, and/or rename a file. Notice that th File class does not contain the methods for reading and writing file contents.

File Methods:

The File class contains methods such as those for establishing a file connection, for deleting a file, for determining if a file exists, and for renaming a file. It does not contain methods for creating/reading/writing data to, or from, the file.

reddot File(String X): creates a File object that refers to the file X; does not contain the methods for creating/reading/writing data to, or from, the file. The file name is a string.

reddot delete(): deletes the designated file permanently. It returns true if the file is deleted.

reddot exists(): returns true if the designated file exists.

reddot rename(): renames the file denoted. Returns true if the renaming is successful.

Return to Unit Menu | | | Terms of Use  |

Notice: These materials are free for your on-line use at this site, but are not free for the taking.
Please do not copy these materials or re-post them to the Internet, as it is copyright infringement.
If you wish hard-copies of these materials, refer to our subscription area,
Help us keep these resources free. Thank you.