The path argument is simply a string with either a full or relative pathname to the file that can be understood by the host operating system.
Here is an example of using the simplest
One thing you may not have noticed about these constructors: since a File object does not represent a file as much as a filename, these constructors do not actually create files. To create a new file with Java, you can
- open a file output stream to the file or
- invoke the createNewFile() method.
The latter only works in Java 2 and later.
In Java 2 and later, construction of a File object includes normalization
. This process reads hardcoded pathnames and attempts to convert them to the conventions of the local platform.
This improves compatibility with code that's making assumptions about filenames.
For instance, if a Windows VM is asked to create a File object with the path
it will actually set the path field to \ public\html\ javafaq\ course\ week2\ index.html.
The reverse process happens on Unix; backslashes are converted to forward slashes.
Because it can only really normalize separators, not filesystem roots, this scheme works better for relative pathnames than absolute ones.