Files Io
Files Io
• Here, directoryPath is the path name of the file; filename is the name
of the file or subdirectory; dirObj is a File object that specifies a
directory; and uriObj is a URI object that describes a file.
Program to Demonstrate
File class and its Methods
Some More Methods of the File Class
Directories
• A directory is a File that contains a list of other files and
directories. When you create a File object that is a
directory, the isDirectory( ) method will return true. In this
case, you can call list( ) on that object to extract the list of
other files and directories inside.
• Syntax :
• String[ ] list( )
• The list of files is returned in an array of String objects.
Creating Directories
• Another two useful File utility methods are mkdir( ) and mkdirs( ).
• The mkdir( ) method creates a directory, returning true on success
and false on failure. Failure can occur for various reasons, such as the
path specified in the File object already exists, or the directory cannot
be created because the entire path does not exist yet.
• To create a directory for which no path exists, use the mkdirs( )
method. It creates both a directory and all the parents of the
directory
I/O Exceptions
• Two exceptions play an important role in I/O handling.
• The first is IOException. As it relates to most of the I/O classes described in
this chapter, if an I/O error occurs, an IOException is thrown.
• In many cases, if a file cannot be opened, a FileNotFoundException is thrown.
• FileNotFoundException is a subclass of IOException, so both can be caught
with a single catch that catches IOException.
try {
// open and access file
} catch( IOException e) { // ... }
finally { // close the file }
The Stream Classes
• Java’s stream-based I/O is built upon four abstract classes:
InputStream, OutputStream, Reader, and Writer.
• InputStream and OutputStream are designed for byte streams.
• Reader and Writer are designed for character streams.
• In general, you should use the character stream classes when
working with characters or strings and
• use the byte stream classes when working with bytes or other binary
objects.
FileInputStream
• The FileInputStream class creates an InputStream that you can use to read bytes
from a file.
• Two commonly used constructors are shown here:
• FileInputStream(String filePath)
• FileInputStream(File fileObj)
• Either can throw a FileNotFoundException. Here, filePath is the full path name of a
file, and fileObj is a File object that describes the file.
• The following example creates two FileInputStreams that use the same disk file and
• each of the two constructors:
• FileInputStream f0 = new FileInputStream("/autoexec.bat")
• File f = new File("/autoexec.bat");
• FileInputStream f1 = new FileInputStream(f);
FileOutputStream
• FileOutputStream creates an OutputStream that you can use to write bytes
to a file.
• It implements the AutoCloseable, Closeable, and Flushable interfaces.
• Four of its constructors are shown here:
• FileOutputStream(String filePath)
• FileOutputStream(File fileObj)
• FileOutputStream(String filePath, boolean append)
• FileOutputStream(File fileObj, boolean append)
• They can throw a FileNotFoundException.
• Here, filePath is the full path name of a file, and fileObj is a File object that
describes the file. If append is true, the file is opened in append mode.
Program to READ From a
FILE and WRITE to a FILE
Program to READ From a FILE and WRITE to a FILE
Continued
Program to print all files in a folder(and sub-folders)
Cont’d ....