Java Reference
In-Depth Information
Chapter 11. Directory and Filesystem
Operations
Introduction
This chapter is largely devoted to one class: java.io.File . The File class gives you the
ability to list directories, obtain file status, rename and delete files on disk, create directories,
and perform other filesystem operations. Many of these would be considered “system pro-
gramming” functions on some operating systems; Java makes them all as portable as pos-
sible.
Note that many of the methods of this class attempt to modify the permanent file store, or
disk filesystem, of the computer you run them on. Naturally, you might not have permission
to change certain files in certain ways. This can be detected by the Java Virtual Machine's (or
the browser's, in an applet) SecurityManager , which will throw an instance of the un-
checked exception SecurityException . But failure can also be detected by the underlying
operating system: if the security manager approves it, but the user running your program
lacks permissions on the directory, for example, you will either get back an indication (such
as false) or an instance of the checked exception IOException . This must be caught (or de-
clared in the throws clause) in any code that calls any method that tries to change the filesys-
tem.
Java 7 introduced a potential replacement for File , called Path , which we will also investig-
ate.
Getting File Information
Problem
You need to know all you can about a given file on disk.
Search WWH ::




Custom Search