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 ...
Get Java For Dummies Quick Reference now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.