Skip to main content
File I/O
- The classes you need to understand in java.io are File, FileReader,
BufferedReader, FileWriter, BufferedWriter, PrintWriter, and Console.
- A new File object doesn't mean there's a new file on your hard drive.
- File objects can represent either a file or a directory.
- The File class lets you manage (add, rename, and delete) files and directories.
- The methods createNewFile() and mkdir() add entries to your file system.
- FileWriter and FileReader are low-level I/O classes. You can use
them to write and read files, but they should usually be wrapped.
- Classes in java.io are designed to be "chained" or "wrapped." (This is a common use of the decorator design pattern.)
- It's very common to "wrap" a BufferedReader around a FileReader or a
BufferedWriter around a FileWriter, to get access to higher-level (more
convenient) methods.
- PrintWriters can be used to wrap other Writers, but as of Java 5 they can be built directly from Files or Strings.
- Java 5 PrintWriters have new append(), format(), and printf() methods.
- Console objects can read non-echoed input and are instantiated using System.console().