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

Apply File Io Operation in Java

This document provides an overview of file input/output (I/O) operations in Java. It discusses streams as an abstraction for reading and writing files or communicating over networks. It describes character stream classes like Reader and Writer that are used to read from and write to sources and destinations. Specific subclasses of Reader and Writer like FileReader, FileWriter, InputStream and OutputStream are mentioned. The File class, which represents files and directories, is also covered along with its common constructors and methods. Object serialization using ObjectOutputStream is briefly explained with an example.

Uploaded by

Edwin Lapat
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Apply File Io Operation in Java

This document provides an overview of file input/output (I/O) operations in Java. It discusses streams as an abstraction for reading and writing files or communicating over networks. It describes character stream classes like Reader and Writer that are used to read from and write to sources and destinations. Specific subclasses of Reader and Writer like FileReader, FileWriter, InputStream and OutputStream are mentioned. The File class, which represents files and directories, is also covered along with its common constructors and methods. Object serialization using ObjectOutputStream is briefly explained with an example.

Uploaded by

Edwin Lapat
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

APPLY FILE I/O

OPERATION IN
JAVA
Abstract streams

► Streams are an abstraction used when reading or writing files, or communicating


over network sockets. ... In the Smalltalk standard library and in other
programming languages as well, a stream is an external iterator. As in Scheme,
streams can represent finite or infinite sequences.
Stream Subclasses

► Character Stream Classes are used to read characters from the source and
write characters to destination.
► There are two kinds of Character Stream classes - Reader classes and Writer
classes.
► Reader Classes - These classes are subclasses of an abstract class, Reader and they are
used to read characters from a source(file, memory or console).
► Writer Classes - These classes are subclasses of an abstract class, Writer and they
used to write characters to a destination(file, memory or console).
Reader and Writer Abstract Classes
Reader
► Reader class and its subclasses are used to read characters from source.
Reader class is a base class of all the classes that are used to read characters from a file,
memory or console. Reader is an abstract class and hence we can't instantiate it but we can use
its subclasses for reading characters from the input stream. We will discuss subclasses of
Reader class with their code, in the next few articles.
Methods of Reader class.

► These methods are of Reader class.


Writer

► Writer class and its subclasses are used to write characters to a file, memory or console. Writer
is an abstract class and hence we can't create its object but we can use its subclasses for writing
characters to the output stream.
Methods of Writer class.

► Methods of Writer class provide support for writing characters to the output
stream. As this is an abstract class. Hence, some undefined abstract methods are
defined in ths subclasses of OutputStream.
Reader and Writer Abstract Subclasses
InputStream Subclasses

The Java InputStream class is the base class (superclass) of all input streams in the Java IO
API. Each subclass of InputStream typically has a very specific use, but can be used as an
InputStream. The InputStream subclasses are:
► ByteArrayInputStream
► FileInputStream
► PipedInputStream
► BufferedInputStream
► FilterInputStream
► PushbackInputStream
► DataInputStream
► ObjectInputStream
► SequenceInputStream
Java OutputStream Subclasses

Here are some of the well-known subclasses of the Java OutputStream class:
► ByteArrayOutputStream
► FileOutputStream
► PipedOutputStream
► BufferedOutputStream
► FilterOutputStream
► DataOutputStream
► PrintStream
► ObjectOutputStream
File Class

File class is used to create, delete, rename, access file or directory in our system. It is
also used to check or set some important properties of a file or directory present on a
physical disk of a computer, for example -
► The last time a file or a directory was modified.
► The length of a file or a directory.
► To check if a file or a directory is writable.
► To check if a file or a directory is read only.
We can even set access restrictions like making a file or a directory read
only/writable.
Constructors :

► A few most used constructors of File class are explained with an example below :
Methods of File class
Object serialization

Serializing an Object
► The ObjectOutputStream class is used to serialize an Object. The following
SerializeDemo program instantiates an Employee object and serializes it to a file.
► When the program is done executing, a file named employee.ser is created. The
program does not generate any output, but study the code and try to determine what the
program is doing.
► Note − When serializing an object to a file, the standard convention in Java is to give
the file a .ser extension.
Example
END

You might also like