Files and Filename Filters

In all of the examples thus far, a string has been used to refer to the file that's involved in a stream operation. This often is sufficient for a program that uses files and streams, but if you want to copy files, rename files, or handle other tasks, a File object can be used.

File, which also is part of the java.io package, represents a file or folder reference. The following File constructors can be used:

  • File(String)—Creates a File object with the specified folder—no filename is indicated, so this refers only to a file folder.

  • File(String, String)—Creates a File object with the specified folder path and the specified name.

  • File(File, String)—Creates a File object with its path represented by the specified File and ...

Get Sams Teach Yourself Java 2 in 21 Days, Second Edition 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.