The File Class
When working with data files in Java, the File class from the java.io 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 java.io package remain adequate for most Java programmers and are adequate for this course.
The File class, which is part of the java.io 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.
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.
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.
delete(): deletes the designated file permanently. It returns true if the file is deleted.
exists(): returns true if the designated file exists.
rename(): renames the file denoted. Returns true if the renaming is successful. |