Question: Hello, I was wondering how to write a program that creates a file named randomdata.dat if it does not exist and then writes 100 random
Hello, I was wondering how to write a program that creates a file named randomdata.dat if it does not exist and then writes 100 random integers into the file. Read the data from the file randomdata.dat, sort it in ascending order and print it out. I then need to make sure that the code handles any checked exceptions. This is the code that I currently have...
package randdat;
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException;
public class RandDat {
public static void main(String[] args) { } public static int[] getArray() { int[] array = new int[100]; for (int i = 0; i < array.length; i++) { array[i] = (int)(Math.random() * 100) + 1; } return array; } public static void readFile() throws IOException { File file = new File("randomdata.dat"); try(BufferedReader reader = new BufferedReader (new FileReader(file))) { } catch(IOException s) { } } public static void writefile() { try(BufferedWriter writer = new BufferedWriter(new BufferedWriter(new FileWriter("randomdata.dat", true)))) { getArray(); writer.close(); } catch(IOException e) { } }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
