Assignment - 8
Aim: File Handling
Problem Statement
Implement a program for maintaining a student records database using File Handling. Student has
Student_id, name, Roll_no, Class, marks and address. Display the data for five students.
i) Create Database
ii)Display Database
iii) Clear Records
iv)Modify record
v)Search Record
Objectives: To understand the concept of Java FileWriter and FileReader classes.
heory:
T
Importance of file handling?
A Stream represents flow of data from one place to another place Input Streams reads or accepts
data Output Streams sends or writes data to some other place All streams are represented as
classes in javaio package The main advantage of using stream concept is to achieve hardware
independence This is because we need not change the stream in our program even though we
change the hardware Streams are of two types in Java:
Byte Streams: Handle data in the form of bits and bytes Byte streams are used to handle any
characters (text), images, audio and video files For example, to store an image file (gif or jpg),
we should go for a byte stream To handle data in the form of 'bytes' the abstract classes:
InputStream and OutputStream are used The important classes of byte streams are:
FileWriter is a class which is in [Link] package that is use to create a file by directly writing
characters. Java FileWriter and FileReader classes are used to write and read data from text files
(they are Character Stream classes). Reading and writing take place character by character, which
increases the number of I/O operations and effects performance of the system. BufferedWriter
can be used along with FileWriter to improve speed of execution.
FileWriter
FileWriter is useful to create a file writing characters into it.
• This class inherits from the OutputStream class.
• The constructors of this class assume that the default character encoding and the default
byte-buffer size are acceptable. To specify these values yourself, construct an
OutputStreamWriter on a FileOutputStream.
• FileWriter is meant for writing streams of characters. For writing streams of raw bytes,
consider using a FileOutputStream.
• FileWriter creates the output file, if it is not present already.
Constructors of FileWriter
1. FileWriter(String filepath)
2. FileWriter(String filepath, boolean append)
3. FileWriter(File fileobj)
Methods of FileWriter
Method Name Description
public void write(String text) Use to write String into file.
public void write(char c) se to write char into file.
public void close() Use to to close the file object.
public void flush() Use to flush the FileWriter contents.
If you will not close the file object then your file data may be lost so don’t forget to close file
object using close() method.
Buffer in java
File Manipulation and operations
Tokenizing the Input Using the Scanner Class
Example
import [Link].*;
class FileWriterTest{
public static void main(String[] args)throws IOException {
FileWriterfw=new FileWriter("[Link]");
BufferedReaderbr=new BufferedReader(new InputStreamReader([Link]));
char ch;
[Link]("Enter Char to Exit '@'");
while( (ch=(char) [Link]()) !='@' ){
[Link](ch);
}
[Link]();
}
}
In the above step a new file will be created every time and previous data will be lost.
FileWriterfw = new FileWriter("[Link]",true);
In this case file will not be create every time, If file already exist in given location then it will
append contents to existing file because mode true is added at the time creating FileWriter object.
FileReader
FileReader class is use to read text from the file.
Constructors of FileReader
1. FileReader(String filepath)
2. FileReader(File fileobj)
Methods of FileReader
1. int read() : Use to return integer value of next character.
2. int read(char buff[]) : Use to up to buffer length.
3. abstract void close() : Use to close the input source
Example
import [Link].*;
class FileWriterTest{
public static void main(String[] args)throws IOException {
FileReaderfr = new FileReader("[Link]");
intch;
[Link]("File contents are:");
while((ch=[Link]())!=-1){
[Link]((char)ch);
}
[Link]();
}
}
uffer in java
B
A buffer is a memory block that is used to store data. Buffer improved the speed of execution
while reading and writing data. We can improve the speed by execution using the following
Buffered class.
Buffer Classes: There are four types of buffer classes which work with Stream classes.
BufferedReader
BufferedWriter
BufferedInputStream
BufferedOutputStream
Implementation
Algorithm for Adding Records:-
1. Start
2. Open the database file.
3. Read data from the user.
4. Print the data to file.
5. Close the file.
6. End
Algorithm for Displaying Records:-
1. Start
2. Open the database file.
3. Read data from the filr.
4. Print the data on screen.
5. Close the file.
6. End
Algorithm for Clearing All Records:-
1. Start
2. Overwrite the database file with a blank file.
3. Close the file.
4. End
onclusion
C
Thus we have implemented File handling concept using java.
Frequently asked Question
1. Give the basic methods in File class?
2. Why to use FileWriter class and give its advantages .
3. Write a File-Copy program which copies the content of one file to another.
Take both the file names from the user
4. How to read data from a file ,using FileReader class?
5. Give usage of BufferedWriter and BufferedReader classes in Java with example
6. Write a code to generate database for Criket player ising file handling operations