Java Reference
In-Depth Information
File Handling and I/O
d O
Java has had input/output (I/O) support since the very first version. However, due
to Java's strong desire for platform independence, the earlier versions of I/O func‐
tionality emphasized portability over functionality. As a result, they were not always
easy to work with.
We'll see later in the chapter how the original APIs have been supplemented—they
are now rich, fully featured, and very easy to develop with. Let's kick off the chapter
by looking at the original, “classic” approach to Java I/O, which the more modern
approaches layer on top of.
Classic Java I/O
The File class is the cornerstone of Java's original way to do file I/O. This abstrac‐
tion can represent both files and directories, but in doing so is sometimes a bit cum‐
bersome to deal with, and leads to code like this:
// Get a file object to represent the user's home directory
File homedir = new File ( System . getProperty ( "user.home" ));
// Create an object to represent a config file (should
// already be present in the home directory)
File f = new File ( homedir , "app.conf" );
// Check the file exists, really is a file & is readable
if ( f . exists () && f . isFile () && f . canRead ()) {
// Create a file object for a new configuration directory
File configdir = new File ( f , ".configdir" );
// And create it
configdir . mkdir ();
Search WWH ::

Custom Search