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

Write a class called FileArray. The FileArray class should have the following me

ID: 3570708 • Letter: W

Question

Write a class called FileArray.
The FileArray class should have the following methods:
a static method named writeArray that takes 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.
A static method named readArray that takes 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.

Please include comments! thank you!

Explanation / Answer

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

class FileArray
{
   static void writeArray (String filename, int[] a)
   {
       BufferedWriter bw = null;
       try
       {
           File file = new File(filename);

           if (!file.exists())
           {
               file.createNewFile();
           }


           bw = new BufferedWriter(new FileWriter(file.getAbsoluteFile()));

           for (int i = 0; i <a.length; i++)
               bw.write(a[i] + " ");

           bw.close();

       }
       catch (IOException e)
       {
           e.printStackTrace();
       }
       finally
       {
           try
           {
               if (bw != null)
                   bw.close();
           }
           catch (IOException ex)
           {
               ex.printStackTrace();
           }
       }
   }

static void readArray (String filename, int[] a)
   {
       BufferedReader br = null;
       String sCurrentLine;
       int i = 0;

       try
       {
           br = new BufferedReader(new FileReader(filename));

           while ((sCurrentLine = br.readLine()) != null)
           {
               a[i] = Integer.parseInt(sCurrentLine);
               i++;
           }

       }
       catch (IOException e)
       {
           e.printStackTrace();
       }
       finally
       {
           try
           {
               if (br != null)
                   br.close();
           }
           catch (IOException ex)
           {
               ex.printStackTrace();
           }
       }
   }

public static void main (String args[])
{
       int a[] = { 10, 45, 67, 89, 120, 38, 29, 17, 90, 76};
       int b[] = new int[10];

       writeArray("test_input.dat", a);
       readArray("test_input.dat", b);
       for (int i = 0; i < b.length; i++)
           System.out.println(b[i]);

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote