File Class
Package: java.io
The File class represents a single file or directory. You can use the File class for basic file-manipulation tasks such as creating new files, deleting files, renaming files, and so on.
It’s important to understand that the File object represents a file that may or may not actually exist on disk. For example, to create a file on disk, you first create a File object for the file. Then, you call the File object’s createNewFile method to actually create the file on disk.
Constructor
|
Constructor |
Description |
|
|
Creates a file with the specified pathname. |
To create a File object, you call the File constructor, passing a string representing the filename as a parameter. Here’s an example:
File f = new File(“myfile.txt”);
Here, the file’s name is myfile.txt, and it lives in the current directory, which usually is the directory from which the Java Virtual Machine (JVM) was started.
If you want to use a directory other than the current directory, you must supply a complete pathname in the parameter string. Bear in mind that pathnames are system-dependent. For example, c:\mydifectory\myfile.txt is valid for Windows systems, for example, but not on Unix or Macintosh systems, which don’t use drive letters and ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access
