Apply File Io Operation in Java
Apply File Io Operation in Java
OPERATION IN
JAVA
Abstract streams
► 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.
► 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