20.6k views
2 votes
Design a class FileArray 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, and then the file should be closed. Write a second static method in the class FileArray 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 written into the array, and then the file should be closed.

User BabakHSL
by
3.7k points

2 Answers

2 votes

Final answer:

The FileArray class should have two static methods, writeArray and readArray, for writing to and reading from a binary file. The writeArray method opens the file, writes the int array's contents, and closes the file. The readArray method reads data from the file into an int array and then closes the file.

Step-by-step explanation:

Class Design for FileArray with writeArray and readArray Methods

The FileArray class is designed to handle binary file operations for integer arrays. The class includes two static methods: writeArray and readArray. The writeArray method is responsible for opening a binary file, writing the contents of an int array to the file, and then closing the file. Conversely, the readArray method opens a binary file, reads its contents into an existing int array, and closes the file upon completion.

Implementation of writeArray

To implement the writeArray method, you would create a method that accepts the file name and the int array reference. Using Java's FileOutputStream and DataOutputStream, you can write the array to the file. Exception handling will be necessary to manage potential I/O errors. After writing all elements, the file is closed to ensure no resources are leaked.

Implementation of readArray

The readArray method also requires the file name and an int array reference to fill with data read from the file. A combination of FileInputStream and DataInputStream can be employed to read the binary data. As with writeArray, exceptions need to be handled, and the file must be closed afterward.

The general pattern for both methods involves opening a stream to the file, performing the read/write operation, handling exceptions, and closing the stream to avoid resource leakage.

User Jibran K
by
3.9k points
2 votes

Answer:

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

public class FileArray

{

public static void writeArray(String fileName, int[] array) throws IOException

{

FileOutputStream fstream = new FileOutputStream(fileName);

DataOutputStream outputFile = new DataOutputStream(fstream);

for(int index = 0; index < array.length; index++)

{

outputFile.writeInt(array[index]);

}

outputFile.close();

}

public static void readArray(String fileName, int[] array) throws IOException

{

FileInputStream fstream = new FileInputStream(fileName);

DataInputStream inputFile = new DataInputStream(fstream);

for(int index = 0; index < array.length; index++)

{

array[index] = inputFile.readInt();

}

inputFile.close();

}

}

Step-by-step explanation:

The above code has the Class named FileArray and Static method as writeArry. and it takes String fileName, int[] array as arguments.String fileName is for the name of the file and int[] array for the reference to the int array. OutputStream and InputStream are abstract classes used to read byte streams to read and write binary data. Content of the file is written to the file.

Second static method of FileArray is wriiten as readArray and it also takes two arguments similar to static method writeArray. In this data is read from the file and written into array.

User Dave Slutzkin
by
3.9k points