0% found this document useful (0 votes)
45 views

Files Io

Input/Output (I/O) streams represent sources of input and destinations of output for a program. Streams can represent various types of sources and destinations like disk files, devices, programs, and memory arrays. They also support different data types including bytes, primitive types, characters, and objects. Input streams are used to read data from a source into a program one item at a time, while output streams are used to write data from a program to a destination one item at a time. Common stream classes in Java include FileInputStream, FileOutputStream, and File which deals directly with files and the file system.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Files Io

Input/Output (I/O) streams represent sources of input and destinations of output for a program. Streams can represent various types of sources and destinations like disk files, devices, programs, and memory arrays. They also support different data types including bytes, primitive types, characters, and objects. Input streams are used to read data from a source into a program one item at a time, while output streams are used to write data from a program to a destination one item at a time. Common stream classes in Java include FileInputStream, FileOutputStream, and File which deals directly with files and the file system.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Input/Output (I/O) Streams

Input/Output (I/O) Streams


Input/Output Streams
• An I/O Stream represents an input source or an output
destination.
• A stream can represent many different kinds of sources and
destinations, including disk files, devices, other programs and
memory arrays
• Streams support many different kinds of data, including simple
bytes, primitive data types, localized characters, and objects.
• Some streams simply pass on data; others manipulate and
transform the data in useful ways.
Input Stream
• A stream is a sequence of data. A program uses an input stream to
read data from a source, one item at a time:

• Inputs: Command Line arguments, File, Keyboard, Network, mouse,


webcam, sensors ……

Reading Information into a Program


Output Stream

• A program uses an output stream to write data to a destination,


one item at time:
• Output : Files, Console, network, speaker, LED, …..

Writing information from a program


I/O Streams
the console
• System.out: stand attached with
ard output stream streams are
• System.in: standar and all these
d input stream automatically
• System.err: stand are created
ard error stream Three streams
Standard Streams
Input Stream Classes
Output Stream Classes
The File CLASS
• It deals directly with Files and the File System while other classes
operate on streams
• The File class does not specify how information is retrieved from or
stored in files; it describes the properties of a file itself.
• A File object is used to obtain or manipulate the information
associated with a disk file, such as the permissions, time, date, and
directory path, and to navigate subdirectory hierarchies.
• A directory in Java is treated simply as a File with one additional
property—a list of filenames that can be examined by the list( ) method.
• The following constructors can be used to create File objects:
• File(String directoryPath)
• File(String directoryPath, String filename)
• File(File dirObj, String filename)
• File(URI uriObj)

• 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 ....

You might also like