FileArray ClassDesign a class that has a static method named writeArray. The met
ID: 3649690 • Letter: F
Question
FileArray ClassDesign a class 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 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
/** FileArray Class Design a class 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 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. */ import java.io.*; import java.util.Scanner; public class FileArray { /////////////////////////////////////////////////////////////////////////////////////////////////////////// /** * pt method * @param s * The pt method is used for a short hand * way to use the System.out.print(); method */ private static void pt(String s) { System.out.print(s); } /////////////////////////////////////////////////////////////////////////////////////////////////////////// /** * WriteArray method * @param num The array that holds the int's to be written to a file * @param fn The file name to be written to * @throws IOException */ public static void WriteArray(int[] num, String fn) throws IOException { //Create the binary output objects. FileOutputStream fstream = new FileOutputStream(fn); DataOutputStream outputFile = new DataOutputStream(fstream); pt("Writing the numbers to " + fn + "..."); // Write the array elements to the file. for (int i = 0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.