Asked by Leslie Alaniz on Jun 05, 2024
Verified
Write a complete Java program that opens a binary file containing integers and displays the contents to the screen.
Binary File
A type of computer file that contains data in binary format, as opposed to text format, making it non-human readable.
Contains Integers
Typically refers to a data structure that is capable of storing integer values.
Displays Content
The act of presenting information, such as text, images, or videos, on a device screen or through another output medium.
- Develop the capability to construct Java statements and programs facilitating elementary file input/output processes.
- Acquire the skill to initiate input streams from textual and binary files utilizing various classes in Java.
Verified Answer
CK
Chetan KommulaJun 10, 2024
Final Answer :
import java.io.ObjectInputStream;
import java.io.FileInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.FileNotFoundException;
public class BinaryInputDemo
{
public static void mainString args[])
{
try
{
ObjectInputStream inputStream =
new ObjectInputStreamnew
FileInputStream"statistics.dat"));
int stat = 0;
try
{
whiletrue)
{
stat = inputStream.readInt);
System.out.printlnstat);
}
}
catchEOFException e)
{
System.out.println"End of file encountered");
}
inputStream.close);
}
catchFileNotFoundException e)
{
System.out.println"Unable to locate file");
}
catchIOException e)
{
System.out.println"Unable to read file");
}
}
}
import java.io.FileInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.FileNotFoundException;
public class BinaryInputDemo
{
public static void mainString args[])
{
try
{
ObjectInputStream inputStream =
new ObjectInputStreamnew
FileInputStream"statistics.dat"));
int stat = 0;
try
{
whiletrue)
{
stat = inputStream.readInt);
System.out.printlnstat);
}
}
catchEOFException e)
{
System.out.println"End of file encountered");
}
inputStream.close);
}
catchFileNotFoundException e)
{
System.out.println"Unable to locate file");
}
catchIOException e)
{
System.out.println"Unable to read file");
}
}
}
Learning Objectives
- Develop the capability to construct Java statements and programs facilitating elementary file input/output processes.
- Acquire the skill to initiate input streams from textual and binary files utilizing various classes in Java.