Java Reference ยท I/O and Files
The File class
Reference for java.io.File: represent file and directory paths, create, list, rename and delete files, and know when to use java.nio.file.Path instead.
java.io.File is the original Java class for file system paths. Despite the name it does not represent an open file or its contents: it is an abstract pathname, a string with helper methods. Creating a File object touches nothing on disk; methods such as exists(), length() or mkdir() ask the file system when you call them.
File is still everywhere in older APIs, but it has weaknesses: many methods return false on failure instead of explaining why, symbolic links and file attributes are poorly supported, and there is no directory walking. For new code use java.nio.file.Path and Files; file.toPath() and path.toFile() convert between them.
File dir = new File("data"); File report = new File(dir, "report.txt"); // parent + child if (!dir.exists()) { dir.mkdirs(); }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
public File(String pathname) public File(String parent, String child) public File(File parent, String child) public File(URI uri)
| Method | Description |
|---|---|
exists(), isFile(), isDirectory() |
Tests against the file system. |
length() |
Size in bytes (0 if missing; unspecified for directories). |
lastModified() |
Milliseconds since the epoch, or 0 if missing. |
canRead(), canWrite(), canExecute() |
Permission checks (advisory; the real operation can still fail). |
getName(), getParent(), getParentFile(), getPath() |
Parts of the pathname (no disk access). |
getAbsolutePath(), getCanonicalPath() |
Absolute form; the canonical form resolves ., .. and links and throws IOException. |
createNewFile() |
Atomically creates an empty file; returns false if it already exists. Throws IOException. |
mkdir(), mkdirs() |
Creates one directory, or the whole chain. Return false on failure. |
delete() |
Deletes a file or an empty directory; returns false otherwise. |
renameTo(File) |
Renames or moves; platform dependent, returns false on failure. |
list(), list(FilenameFilter), listFiles(...) |
Names or File objects in a directory; null if it is not a directory or an I/O error occurs. |
createTempFile(prefix, suffix) |
Creates an empty file in the temp directory; prefix needs at least 3 characters. |
deleteOnExit() |
Deletes the file when the JVM exits normally. |
toPath() |
Converts to java.nio.file.Path. |
Usage notes
- Failures are quiet.
delete(),mkdir()andrenameTo()returnfalsewithout a reason.Files.delete,Files.createDirectoryandFiles.movethrow exceptions with details. - Check for
nullfromlist()andlistFiles(): a missing directory and an unreadable one both givenull, not an empty array. - Order is undefined. Sort the results of
list()if you need stable output. - Relative paths resolve against the JVM's current directory (
user.dir), which may not be where you expect. - Check-then-act races:
if (!f.exists()) f.createNewFile()can race with other processes; callcreateNewFile()alone and look at its result. - No recursion. Deleting a non-empty directory needs your own walk (see
Files.walkorwalkFileTree). Fileis immutable and safe to share between threads; the files it names are not protected.- Path separators: use
File.separatoror the two-argument constructors rather than hard-coded separator characters when building paths by hand. - Constructors throw
NullPointerExceptionfor anullpath string.