Design a class (FileArrayClass) that has a static method named WriteArray. The m
ID: 3538178 • Letter: D
Question
Design a class (FileArrayClass) that has a static method named WriteArray. The method should take two arguments: the name of a file and a reference to an int array. The file should be opened as a binary file, the contents of the array should be written to the file, an then the file should be closed.
Write a second method in the class named readArray . the method should take two arguments : the name of a file and a reference to an int array. The file should be opened, data should be read from the file and stored in the array, and then the file should be closed. Demonstrate both methods in a program.
Explanation / Answer
Java code:
RandWBinaryFile.java
//import required packages
import java.io.*;
import java.util.Scanner;
//class RandWBinaryFile
public class RandWBinaryFile
{
//method to write the array elemets to a binary file
public static void writeArray(String filename, int[] num) throws IOException
{
//Create the binary output file object
FileOutputStream fstream = new FileOutputStream(filename);
DataOutputStream outputFile = new DataOutputStream(fstream);
//display the numbers stored in the array
System.out.println("The numbers in the array are :");
for(int i = 0; i < num.length; i++)
System.out.print(num[i]+", ");
System.out.println(" " + "Writing the numbers in the array to the binary-file " + filename + "...");
// Write the array elements to the file
for(int i = 0; i < num.length; i++)
outputFile.writeInt(num[i]);
System.out.print("Done. ");
// Close the file
outputFile.close();
}
private static void readArray(String filename, int[] num2) throws IOException
{
boolean endOfFile = false; // End of file flag
// Create the input binary file object
FileInputStream fstream = new FileInputStream(filename);
DataInputStream inputFile = new DataInputStream(fstream);
System.out.println(" Reading numbers from file " + filename + ".");
// Read the contents of the file
int i = 0;
while (!endOfFile)
{
try
{
num2[i] = inputFile.readInt();
i++;
}
//to handel exception when the file not found
catch (FileNotFoundException e)
{
System.out.print("File not found");
}
//to handel exception when end of file reached
catch (EOFException e)
{
endOfFile = true;
}
}
System.out.println("Done. ");
// Close the file.
inputFile.close();
}
public static void main(String[] args)throws IOException
{
Scanner input = new Scanner(System.in);
// The num array hold the numbers to be written to a file.
int[] num = {2,4,6,8,10,12,14,16};
String filename;
int[] num2 = new int[num.length];
//read the output file name from the user
System.out.print("Enter the file name to perform write and read operations: ");
filename = input.nextLine();
//calling methods
writeArray(filename, num);
readArray(filename, num2);
System.out.println("Display the numbers stored in the array read from the file :");
for(int i = 0; i < num2.length; i++)
System.out.print(num2[i] + ", ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.