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.