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

File Handling

The document outlines the differences between recursion and iteration, highlighting that recursion involves a function calling itself while iteration uses repeated instructions. It also covers file handling operations, including reading and writing to text and binary files, and provides example Java programs for these operations. Additionally, it describes the use of buffers in file handling and includes a method for checking product availability in a binary file.

Uploaded by

Ansh Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

File Handling

The document outlines the differences between recursion and iteration, highlighting that recursion involves a function calling itself while iteration uses repeated instructions. It also covers file handling operations, including reading and writing to text and binary files, and provides example Java programs for these operations. Additionally, it describes the use of buffers in file handling and includes a method for checking product availability in a binary file.

Uploaded by

Ansh Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Difference Between Recursion and Iteration

Recursion Iteration
1. Function calls itself. 1. A set of instructions repeatedly
2. Size of code is smaller than executed.
iteration. 2. Size of code is larger than
3. It takes more time for recursion.
execution 3. It takes less time for execution

File handling

File Operations:
1. File Input(Reading from file)
2. File Output(Writing to the file)

Types of File:
1. Text or Character File
2. Binary File

Difference between Text and Binary File


Text File Binary File
1. It stores characters as per specific 1. It stores data in machine
character encoding scheme. readable form.
In case of Java Unicode.

Standard input/Output Process:

Keyboard Program Monitor

File Handling Page 1


File Input Process
File File I/P Stream Program Standard O/P Stream Monitor

File Output Process


Keyboard Standard I/P Stream Program File O/P Stream File

Buffer:- A buffer is a temporary storage used to hold data.


Input Buffer: An input buffer is used for reading a large chunk of
data from a stream. The buffer is then accessed as needed and,
when emptied, another chunk of data is read from the stream into
the buffer.
Output Buffer:-An output buffer is used to store up data to be
written to a stream. Once the buffer is full, the data is sent to the
stream all at once and the buffer is emptied to receive more data.

Name the stream class to perform following operations:


Text File
1. To write on text file(File Output) - FileWriter Class
2. To read from text file(File Input) - FileReader Class

Binary Files:
1. To write on text file(File Output) - FileOutputStream Class
2. To read from text file(File Input) - FileInputStream Class

File Handling Page 2


Program to write on Text File

import java.io.*;
import java.util.Scanner;
class textout
{
public static void main(String arg[])throws IOException
{
Scanner sc=new Scanner(System.in);
FileWriter fw=new FileWriter("Students.txt");
BufferedWriter bw=new BufferedWriter(fw);
PrintWriter pout=new PrintWriter(bw);
for(int i=1;i<=5;i++)
{
System.out.println("Enter name");
String name =sc.nextLine();
pout.println(name);
}
pout.close();
}
}

Program to read from Text File

import java.io.*;
class textin
{
public static void main(String arg[])throws IOException
File Handling Page 3
public static void main(String arg[])throws IOException
{
FileReader fr=new FileReader("Students.txt");
BufferedReader br= new BufferedReader(fr);
String name;
while((name=br.readLine())!=null)
{
System.out.println(name);
}
br.close();
}
}

Program to write on Binary File

import java.io.*;
import java.util.Scanner;
class binout
{
public static void main(String arg[])throws IOException
{
Scanner sc=new Scanner(System.in);
int rno;
double marks;
FileOutputStream fout=new FileOutputStream("Marks.dat");
DataOutputStream dout=new DataOutputStream(fout);
for(int i=1;i<=5;i++)
{
System.out.println("Enter roll number");
rno=sc.nextInt();
System.out.println("Enter marks");
marks=sc.nextDouble();
File Handling Page 4
marks=sc.nextDouble();
dout.writeInt(rno);
dout.writeDouble(marks);
}

dout.close();
fout.close();
}
}

Program to read from Binary File


import java.io.*;
import java.util.Scanner;
class binInp
{
public static void main(String arg[])throws IOException
{
Scanner sc=new Scanner(System.in);
FileInputStream fr=new FileInputStream("Marks.dat");
DataInputStream dr=new DataInputStream(fr);
boolean eof=false;
while(!eof)
{
try
{
int rno;
double marks;
rno=dr.readInt();
marks=dr.readDouble();
System.out.println("Roll Number="+rno);
System.out.println("Mark="+marks);
}
catch(EOFException e1)
{
File Handling Page 5
{
System.out.println("end of file");
eof=true;
}
}
}
}

A binary file named "Invoice.dat" contains the product code(pc), unit


price(up) and quantity (q) for number of items. Write a method to
accept a product code 'p' and check the availability of the product and
display with an appropriate message. The Method declaration is as
follows.
void findProduct(int p)

Ans:
void findProduct(int p)
{
Scanner sc=new Scanner(System.in);
FileInputStream fr=new FileInputStream("Invoice.dat");
DataInputStream dr=new DataInputStream(fr);
boolean eof=false;
int f=0;
while(!eof)
{
try
{
int pc;
double up;
int q;
pc=dr.readInt();
up=dr.readDouble();
q=dr.readInt();
File Handling Page 6
q=dr.readInt();
if(p==pc && q>0)
{
System.out.println("Available quantity product="+q);
f=1;
break;
}
catch(EOFException e1)
{
System.out.println("end of file");
eof=true;
}
}
if(f==0)
System.out.println("Item is not available");
}

File Handling Page 7

You might also like