In: Computer Science
JAVA Program:
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
public class WriteDoubleToFile {
public static void main(String[] args) {
String strFilePath = "E://WriteDouble.txt";
double []grades = new double[8];
double []gradesReadFromFile = new double[8];
int i;
Scanner sc = new Scanner(System.in);
for(i=0;i<8;i++){
System.out.print("Enter the grade
between 0 and 10 for student "+(i+1)+": ");
grades[i] = sc.nextDouble();
}
try
{
FileOutputStream fos = new
FileOutputStream(strFilePath);
DataOutputStream dos = new
DataOutputStream(fos);
for(i=0;i<8;i++)
dos.writeDouble(grades[i]);
dos.close();
FileInputStream fin = new
FileInputStream(strFilePath);
DataInputStream din = new
DataInputStream(fin);
for(i=0;i<8;i++)
gradesReadFromFile[i]=din.readDouble();
din.close();
}
catch (IOException e)
{
System.out.println("IOException : " + e);
}
double highest = gradesReadFromFile[0];
double lowest = gradesReadFromFile[0];
for(i=1;i<8;i++){
if(gradesReadFromFile[i]>highest)
highest=gradesReadFromFile[i];
if(gradesReadFromFile[i]<lowest)
lowest=gradesReadFromFile[i];
}
System.out.println("Highest grade: "+highest);
System.out.println("Lowest grade: "+lowest);
}
}
Output: