File Constructors

There are three constructor methods in java.io.File. Each takes some variation of a filename as an argument(s). The simplest is

public File(String path)
path is simply a String with either a full or relative pathname to the file which can be understood by the host operating system. For example,

  File f1 = new File("25.html");
  File f2 = new File("/etc/passwd");
If you like you can separate the path and the filename using the

public File(String path, String name)
constructor. Here name is the filename and path is the name of the directory that contains the file. For example,

File f2 = new File("/etc", "passwd");
Finally there's

public File(File dir, String name)
which is like the previous constructor except that now dir is a File object itself instead of a String.

Some methods in other classes also return File objects, most notably the java.awt.FileDialog methods. The File objects returned by these methods will conform to the conventions of the host operating system.


Previous | Next | Top
Last Modified November 17, 1997
Copyright 1997 Elliotte Rusty Harold
elharo@metalab.unc.edu