import java.io.
*; public void run() {
import java.util.*; List<Student> students = new
ArrayList<>();
class Student {
private String name; try {
private String rollNumber; // Reading the file and populating the
private int[] marks = new int[3]; // Marks 1, students list
2, and 3 synchronized (lock) {
try (BufferedReader reader = new
public Student(String name, String BufferedReader(new FileReader(inputFile))) {
rollNumber, int marks1, int marks2, int String line;
marks3) { while ((line = reader.readLine()) !=
this.name = name; null) {
this.rollNumber = rollNumber; String[] parts = line.split(",");
this.marks[0] = marks1; if (parts.length == 5) { // 1
this.marks[1] = marks2; name, 1 rollNumber, 3 marks
this.marks[2] = marks3; String name = parts[0].trim();
} String rollNumber =
parts[1].trim();
public int[] getMarks() { int marks1 =
return marks; Integer.parseInt(parts[2].trim());
} int marks2 =
Integer.parseInt(parts[3].trim());
public double getAverageMarks() { int marks3 =
return (marks[0] + marks[1] + marks[2]) / Integer.parseInt(parts[4].trim());
3.0; students.add(new
} Student(name, rollNumber, marks1, marks2,
marks3));
@Override }
public String toString() { }
return "Student{" + }
"name='" + name + '\'' + }
", rollNumber='" + rollNumber + '\'' +
", marks=" + Arrays.toString(marks) + // Calculate total marks and average
'}'; double totalMarks = 0;
} for (Student student : students) {
} totalMarks +=
student.getAverageMarks();
class FileProcessor implements Runnable { }
private final String inputFile;
private final String outputFile; double averageMarks =
private static final Object lock = new students.isEmpty() ? 0 : totalMarks /
Object(); students.size();
public FileProcessor(String inputFile, String // Write the average to the
outputFile) { corresponding output file
this.inputFile = inputFile; synchronized (lock) {
this.outputFile = outputFile; try (BufferedWriter writer = new
} BufferedWriter(new FileWriter(outputFile))) {
writer.write("Average Marks: " +
@Override averageMarks);
} }
}
// Create and start a thread for each
System.out.println("Processed file: " + input-output file pair
inputFile + " | Average Marks: " + List<Thread> threads = new ArrayList<>();
averageMarks); for (int i = 0; i < inputFiles.length; i++) {
Thread processor = new Thread(new
} catch (IOException e) { FileProcessor(inputFiles[i], outputFiles[i]));
System.err.println("Error processing file processor.start();
" + inputFile + ": " + e.getMessage()); threads.add(processor);
} }
}
} // Wait for all threads to finish
try {
public class File { for (Thread thread : threads) {
public static void main(String[] args) { thread.join();
// List of student files and their }
corresponding output files } catch (InterruptedException e) {
String[] inputFiles = {"students1.txt", System.err.println("Thread interrupted:
"students2.txt", "students3.txt"}; " + e.getMessage());
String[] outputFiles = {"average1.txt", }
"average2.txt", "average3.txt"};
System.out.println("All files processed.
String[][] data = { Check individual output files for results.");
{"John Doe", "R123", "85", "90", "88"}, }
{"Jane Smith", "R124", "78", "82", }
"80"},
{"Alice Brown", "R125", "92", "88",
"91"},
{"Bob White", "R126", "70", "75", "68"},
{"Charlie Black", "R127", "88", "92",
"85"}
};
// List of student files to write
String[] files = {"students1.txt",
"students2.txt", "students3.txt"};
// Write sample data into each file
for (String file : files) {
try (BufferedWriter writer = new
BufferedWriter(new FileWriter(file))) {
for (String[] studentData : data) {
writer.write(String.join(", ",
studentData));
writer.newLine();
}
} catch (IOException e) {
System.err.println("Error writing to
file " + file + ": " + e.getMessage());
}