Text file gradetxt contains 10 students grade Each line with
Text file “grade.txt” contains 10 students grade. Each line with one grade.
Read all grades from the file “grade.txt” and write them as integers into a binary file (binaryGrade.dat).
Read grades from the binary file “binaryGrade.dat” and compute and display the average grade. Using Standard java code, write this program.
Solution
Hi, Please find my implementation.
Please let me know in case of any issue.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class GradeProgram {
public static void main(String[] args) throws IOException {
// opening grade.txt
FileReader fr = new FileReader(\"grade.txt\");
BufferedReader br = new BufferedReader(fr);
// opening binaryGrade.dat
FileWriter fw = new FileWriter(\"binaryGrade.dat\");
BufferedWriter bw = new BufferedWriter(fw);
String line;
// reading from grade.txt and writing to binaryGrade.dat
int i=1;
while(i <= 10){
line = br.readLine();
bw.write(line);
bw.newLine();
i++;
}
bw.close();
// now opening binaryGrade.dat file
fr = new FileReader(\"binaryGrade.dat\");
br = new BufferedReader(fr);
int sum = 0;
i = 1;
// reading from binaryGrade.dat and adding grade value in sum
while(i <= 10){
line = br.readLine();
sum += Integer.parseInt(line.trim());
i++;
}
br.close();
fr.close();
System.out.println(\"Average: Grade: \"+(sum/10.0));
}
}
/* grade.txt
54
23
76
87
90
12
65
67
56
89
*/
/*
Sample run:
Average: Grade: 61.9
*/



