Question: Java: Find max and minimum int in a binary file import java.util.*; import java.io.*; public class WriteBinFile { public static void main(String[] args) { FileOutputStream

Java: Find max and minimum int in a binary file

import java.util.*;

import java.io.*;

public class WriteBinFile {

public static void main(String[] args) {

FileOutputStream fos = null;

ObjectOutputStream oss= null;

try {

oss= new ObjectOutputStream(new FileOutputStream("ints.bin"));

oss.writeInt(22);

oss.writeInt(33);

oss.writeInt(-34);

oss.writeInt(109);

oss.writeInt(0);

oss.writeInt(-89);

oss.writeInt(4);

oss.close();

}

catch(FileNotFoundException e) {

System.out.println("File not Found.");

System.exit(0);

} catch (IOException e) {

System.out.println(e.getMessage());

}

}

}

import java.util.*;

import java.io.*;

import java.text.DecimalFormat;

import java.text.NumberFormat;

public class ReadBinFile {

public static void main(String[] args) {

FileInputStream fis = null;

ObjectInputStream ois= null;

int x=1;

int largeInt=0;

int smallInt=0;

try {

ois= new ObjectInputStream(new FileInputStream("ints.bin"));

double avg;

int counter = 0;

int temp= ois.readInt();

while(true) {

counter++;

int number = ois.readInt();

if(number>temp) {

largeInt= number;

}

else {largeInt= temp;}

if(number

smallInt= number;

}

else {smallInt= temp;}

temp= number;

}

}

catch(FileNotFoundException e) {

System.out.println("File not Found.");

System.exit(0);

} catch (IOException e) {

System.out.println("End of file-----");

}

NumberFormat formatter = new DecimalFormat("#0.00");

System.out.println("The largest number encountered: "+largeInt);

System.out.println("The smallest number encountered: "+smallInt);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!