Experiment 7: File Handling
Theory:
File Handling is an integral part of any programming language as file handling
enables us to store the output of any program in a file and allows us
to perform certain operations on it. In simple words, file handling means reading
and writing data to a file.
1. File handling in Java allows reading, writing, and manipulating files using classes from
java.io and java.nio packages.
2. The File class provides methods to create, delete, and check file properties like
existence, readability, and writability.
3. FileWriter and BufferedWriter are used for writing data to a file efficiently.
4. FileReader and BufferedReader are used for reading data from a file.
5. Proper buffer usage, such as using BufferedReader and BufferedWriter, improves file-
handling efficiency by reducing direct disk access.
6. File handling operations should always check for file existence before performing read
or write actions to avoid runtime errors.
7. Java allows both absolute and relative file paths, where absolute paths specify the full
directory location and relative paths depend on the program’s execution directory.
8. Writing to a file can either overwrite existing content or append to it using the append
parameter in FileWriter.
9. Java supports directory operations such as creating (mkdir()), deleting (delete()), and
listing files (listFiles()).
10. Using Files.copy() and Files.move(), Java provides easy ways to copy and move files
without manually handling streams.
Akshat Pawar 23B-IT-005
1)
import java.util.Scanner;
public class meee {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter Basic Pay (BP): ");
double BP = scanner.nextDouble();
double DA = 0.34 * BP;
double HRA = 0.18 * (DA + BP);
double TA = 3600 + (0.34 * 3600);
double Gross = BP + DA + HRA + TA;
double GPF = 0.10 * Gross;
double Tax = 0.20 * Gross;
double Deductions = GPF + Tax;
double Net = Gross - Deductions;
System.out.println("\n---- PAY SLIP ----");
System.out.println("Basic Pay: " + BP);
System.out.println("DA (34% of BP): " + DA);
System.out.println("HRA (18% of DA+BP): " + HRA);
System.out.println("TA: " + TA);
System.out.println("Gross Salary: " + Gross);
Akshat Pawar 23B-IT-005
System.out.println("Deductions:");
System.out.println(" GPF (10% of Gross): " + GPF);
System.out.println(" Tax (20% of Gross): " + Tax);
System.out.println("Net Salary: " + Net);
scanner.close();
Output:
2)
import java.io.*;
public class meee {
public static void main(String[] args) {
String inputFile = "PaySlip.txt";
String outputFile = "CopiedPaySlip.txt";
try {
FileReader reader = new FileReader(inputFile);
Akshat Pawar 23B-IT-005
FileWriter writer = new FileWriter(outputFile);
int ch;
while ((ch = reader.read()) != -1) {
writer.write(ch);
reader.close();
writer.close();
System.out.println("File copied successfully!");
} catch (IOException e) {
System.out.println("An error occurred: " + e.getMessage());
Output:
CONCLUSION: Java programs based on File Handling were compiled and executed
successfully.
Akshat Pawar 23B-IT-005