Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

//This is Java Question import java.io.*; /** This program demonstrates a soluti

ID: 2247692 • Letter: #

Question

//This is Java Question

import java.io.*;

/**
This program demonstrates a solution to the
FileArray Class programming challenge.
*/

public class FileArrayDemo
{
public static void main(String[] args)
{
int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8 };
int[] test = new int[8];

}
}

Name the file with the two static methods FileArray.java. Modify the FileArrayDemo.java file to read in the numbers array to a binary file, then the file should be read to extract the data in the file and read it into the test array. Then printout the test array to the screen.

Design a class that has a static method named writeArray. The method should take two arguments: the name of a file and a refernce 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 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 method in a program.

Explanation / Answer

The program given below demonstrates how to write and read (int ) in binary file. Please don't forget to rate the answer if it helped. Thank you very much.

//This is Java Question

import java.io.*;

/**

   This program demonstrates a solution to the

   FileArray Class programming challenge.

*/

public class FileArrayDemo

{

   private static int readArray(String filename, int[] nums) throws IOException

   {

       System.out.println("Reading array from file " + filename);

       DataInputStream infile = new DataInputStream(new FileInputStream(new File(filename)));

       //first read the total number of ints

       int n = infile.readInt();

      

       //now write each individual number

       for(int i = 0; i < n; i++)

           nums[i] = infile.readInt();

       infile.close();

       return n;

   }

   private static void writeArray(String filename, int[] nums) throws IOException

   {

       System.out.println("Writing array to file " + filename);

       DataOutputStream outfile = new DataOutputStream(new FileOutputStream(new File(filename)));

       //first write the length of array, so that when reading back we know how many elements to read back

       outfile.writeInt(nums.length);

      

       //now write each individual number

       for(int i = 0; i < nums.length; i++)

           outfile.writeInt(nums[i]);

       outfile.close();

   }

public static void main(String[] args)

{

int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8 };

int[] test = new int[8];

String filename = "numbers.bin";

  

try {

           writeArray(filename, numbers); //write numbers array to file

           int n = readArray(filename, test); //read from file into test array

          

           System.out.println("printing test array");

           for(int i = 0; i < n; i++)

               System.out.println(test[i]);

       } catch (IOException e) {

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

       }

}

}

output

Writing array to file numbers.bin
Reading array from file numbers.bin
printing test array
1
2
3
4
5
6
7
8