problem 1-
problem 1-
Problem:
Write a Java program that reads a text file and performs the following tasks:
2. Counts Empty Lines: Counts the number of lines that are empty (contain only whitespace or
are completely blank).
3. Counts Words: Counts the total number of words in the file (splitting the lines by whitespace).
4. Handles Errors Gracefully: Provides informative error messages if the file is not found or if
there are any I/O errors. The program should not terminate abruptly. It should handle
NullPointerException if the filename is null or invalid.
5. Efficient Processing: Processes the file in a memory-efficient way, suitable for even relatively
large files. The whole file should not be loaded into memory at once.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
Expected Output:
Total lines: 4
Empty lines: 1
Total words: 20
Problem 2:
Write a Java program that writes a large number of lines to a file using a BufferedWriter, but
with these improvements:
1. Efficient Buffering: Optimize the BufferedWriter to write data efficiently, minimizing disk I/O
operations, especially when writing a very large number of lines.
2. Robust Error Handling: Implement comprehensive error handling to catch and report various
exceptions that might occur during file writing, such as IOException, NullPointerException, and
IllegalArgumentException. The program should not crash but instead give informative error
messages.
3. Progress Indication: Add a mechanism to display the progress of the file writing (e.g.,
percentage complete) to the user, providing feedback during long operations.
4. Customizable Line Content: Allow the user to specify the content of each line via a command-
line argument. If no argument is provided, use a default line content.
5. File Existence Check: Check if the output file already exists. If it does, either prompt the user
for confirmation to overwrite or choose an alternative filename to prevent accidental data loss.
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;
if (Files.exists(Paths.get(filename))) {
Scanner scanner = new Scanner(System.in);
System.out.print("File already exists. Overwrite? (y/n): ");
if (!scanner.nextLine().equalsIgnoreCase("y")) {
System.out.println("Operation cancelled.");
return;
}
scanner.close();
}
3. Run (example to write 50000 lines with custom content): java EfficientBufferedWriter 50000
"My custom line"
The output will show progress updates as the file is written, and the output.txt file will be created
(or overwritten if it already exists and the user confirms).
problem 3:
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
This code will create a file named students.csv with the following content:
StudentID,Name,Grade
1,Alice,90
2,Bob,85
3,Charlie,95
4,David,78
5,Eve,88