0% found this document useful (0 votes)
11 views8 pages

File Handling

Uploaded by

Dharani Dharani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views8 pages

File Handling

Uploaded by

Dharani Dharani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

File Handling

● File Handling refers to the process of working with files.


● It involves reading data from files, writing data to files, and manipulating files in
various ways
● Files are used to store information on computer storage media such as hard
drive or solid-state drive
● File handling involves the following operations
○ Opening a file: Before you can read data from a file or write data to a file,
you need to open it.
○ Reading from a file: once a file is opened, you can read its contents.
○ Writing to a file: After opening a file, you can write data to it, this includes
creating a new file or appending data to an existing file.
○ Closing a file: when you are done working with a file, you should close it.
Closing a file releases system resources and ensures that any changes
made to the file are saved. Failing which can result in data loss or
resource leak.
File:
● It is a predefined class present in the io package.
● By using this class, we can create a new file, we can also delete a file.
Constructors:
● File(String)
Methods:
● boolean createNewFile()
● boolean canRead()
● boolean canWrite()
● boolean delete()
● boolean setReadOnly()
● boolean setWriteable(boolean)
FileOutputStream:
● It is a predefined class present in the io package.
● The primary purpose of this class is to write or transfer the data into the file by
opening the file in write mode.
Constructors:
● FileOutputStream(String)
● FileOutputStream()
Methods:
● void write(int)
● void write(byte[])
● void close()

FileInputStream:
● It is a predefined class present in the io package.
● The primary purpose of this class is to read or retrieve the data from a particular
file by opening the file in read mode.
Constructors:
● FileInputStream(String)
Methods:
● Int read()
● int read(byte[])
● void close()

BufferedOutputStream:
● It is a predefined class present in the io package.
● Using this class, we can transfer the data with a buffer facility.
● A buffer is a temporary memory space, while transferring the data, it will also be
stored in the buffer.

Constructors:
● BufferedOutputStream(OutputStream)
Methods:
● void write(int)
● void write(byte[])
● Void flush()

BufferedInputStream:
● It is a predefined class present in the io package.
● The primary purpose of this class is to read or retrieve the data and we can get
the data from the buffer.
Constructors:
● BufferedInputStream(InputStream)
Methods:
● int read()
● int read(byte[])
● void flush()
● void close()

SequenceInputStream:
● It is a predefined class present in the io package.
● The primary purpose of this class is to read or retrieve data from multiple files at
a time.
Constructors:
● SequenceInputStream(InputStream, InputStream)
● SequenceInputStream(Enumeration)
Methods:
● int read()
● int read(byte[])
● void close()
ByteArrayOutputStream:
● It is a predefined class present in the io package.
● The primary purpose of this class is to write or transfer common data or similar
data into multiple files
● It will transfer the data in the form of an array of bytes.
Constructors:
● ByteArrayOutputStream()
Methods:
● void write(int)
● void write(byte[])
● void writeTo(OutputStream)
● void close()

File Writer:
● It is a predefined class present in the io package.
● The main purpose of this class is to write a stream of characters into a file, here
we can send the String values directly without converting them into bytes. It is an
extended class of the Writer class.
Constructors:
● FileWriter(String)
Methods:
● void write(String)
● void write(char)
● void write(char[])
● void flush()
● void close()

File Reader:
● It is a predefined class present in the io package.
● The main purpose of this class is to read the Stream of Characters.
Constructors:
● FileReader(String)
Methods:
● Int read()
● void close()

Buffered Writer:
● It is a predefined class present in the io package.
● The functionality of this class is to provide a buffered system for efficient writing
of data.
Constructors:
● BufferedWriter(Writer)
Methods:
● void write(int)
● void write(String)
● void write(char[])
● void write(byte[])
● void close()

Buffered Reader:
● It is a predefined class present in the io package.
● It will provide buffer functionality for the Reader class.
Constructors:
● BufferedReader(Reader)
Methods:
● Int read()
● String readLine()
● byte[] read(byte[])
● void close()
Functional Interface:
An Interface which has only one method is called a Functional Interface

Marker Interface:
An Interface that does not have any properties in it is called a Marker Interface.

Serialization:
The process of transferring or sending n number of bytes of data from a Java
Application to an external file in terms of an object is called Serialization.
De-Serialization:
It is the process of reading or retrieving n number of bytes of data from a text file
to a Java Application.

● By using Serialization we are able to transfer n number of bytes of data and by


using De-Serialization we are able to read/retrieve n number of bytes of data in
terms of an object with more security.
● If we want to provide functionality for both Serialization and De-Serialization, we
have to use a predefined interface called ‘Serializable’.
● Serializable is one of the Marker Interfaces.
● The Marker Interface is nothing but an Interface that is empty with no data
members and fields (variables and methods) and it will provide run-time behavior
for its implementation classes.
● To implement both serialization and de-serialization, we have to follow the below
steps.
● Initially, we need to create a serializable subclass.
● Then we need to implement the Serialization process.
● Then we need to implement the De-Serialization process

Steps and guidelines to implement Serializable Subclass:


● Initially, we need to create a user-defined package.
● Then we need to import the necessary packages.
● We need to create a user-defined class, it has to implement the Serializable
interface.
● We need to create user-defined private variables without values.
● Then we need to provide user-defined values for these private variables using
setter and getter methods.

Steps to implement the Serialization Process:


● Initially, we have to import necessary or required packages then we need to
create a user-defined class.
● Now we need to open the text file in write mode by using the FileOutputStream
class.
● Now we need to create an object for the ObjectOutputStream class by providing
the object of FileOutputStream as a parameter.
● Now we need to create an object for the Serializable subclass.
● We need to provide user-defined values for private variables of the Serializable
subclass with respect to an Object.
● Now we need to transfer or write the object of the Serializable subclass by
invoking a method called writeObject with respect to the Object of the
ObjectOutputStream class.
● Now, we need to close or release the resource.

Steps to implement the De-Serialization process:


● Initially, we have to import the necessary packages.
● We need to create a user-defined class.
● Now we need to open the text file in read mode by using the FileInputStream
class.
● Now, we need to create an object for the ObjectInputStream class by providing
the object of the FileInputStream class as a parameter.
● Now, we will be able to read the values from the text file by using a method like
readObject with respect to the object of the ObjectInputStream class by
performing type cast from the object into a serializable subclass object.
● Now we need to display the values by invoking the getter method with respect to
the object of the serializable subclass.
● Then we need to close the resource.

You might also like