Answer:
See Explaination
Step-by-step explanation:
// for generating random numbers import java.util.Random; // read and write to a file and handling exceptions import java.io.*; // reading data from a file import java.util.Scanner; // storing data into ArrayList import java.util.ArrayList; class Sample { int[] arr; public int[] generateSales() { // creating an object for Random class Random r = new Random(); // creating an array of size 10 arr = new int[10]; for(int i=0;i<arr.length;i++) { // generating random numbers and storing into an array arr[i] = r.nextInt(100); } for(int i=0;i<arr.length;i++) { // printing the array data in console System.out.println(arr[i]); } // returning integer array return arr; } // Writing to a file public String writeToFile(int[] tarr) { // fName for storing file name String fName = null; // try-catch for catching any unwanted exception try { File file = new File("numbers.txt"); // file.createNewFile() creates the file if(file.createNewFile()) { System.out.println("File created Successfully"); } // if file exists else block will be executed else { System.out.println("File already exists'"); } // storing the filename fName = file.getName(); FileWriter myWriter = new FileWriter(fName); for(int i=0; i<tarr.length;i++) { // writing to a file myWriter.write(arr[i]+"\\"); } // closing the file myWriter.close(); System.out.println("Successfully wrote to the file."); } catch (IOException e) { System.out.println("An error occurred."); e.printStackTrace(); } // returning the file name return fName; } // reading data from a file public ArrayList<Integer> readNumbers(String fileName) { // ArrayList to store integer type data ArrayList<Integer> list = new ArrayList<Integer>(); // try-catch to handle FileNotFoundException try { File f = new File(fileName); // reading data from a file Scanner sc = new Scanner(f); while (sc.hasNextInt()) { // storing data into a list list.add(sc.nextInt()); } // closing the file sc.close(); } catch (FileNotFoundException e) { System.out.println("An error occurred."); e.printStackTrace(); } // returning ArrayList return list; } // Printing the data of ArrayList to console public void printResults(ArrayList<Integer> intList) { for(int i=0;i<intList.size();i++) { System.out.println(intList.get(i)); } } // main method public static void main(String[] args) { ArrayList<Integer> listOInt = new ArrayList<Integer>(); // creating an object for Sample class Sample s = new Sample(); int[] rarr; // calling generateSales method and storing the returned int array rarr = s.generateSales(); // calling writeToFile method and storing the returned fileName String fileName = s.writeToFile(rarr); // calling readNumbers method and storing the returned ArrayList listOInt = s.readNumbers(fileName); // calling printResults to display the data of listOInt s.printResults(listOInt); } }