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

(Write/read data) Write a program to create a file named Exercise12_15.txt if it

ID: 3803910 • Letter: #

Question

(Write/read data) Write a program to create a file named Exercise12_15.txt if it does not exist. Write 100 integers created randomly into the file using text I/O. Integers are separated by spaces in the file. Read the data back from the file and display the data in increasing order.

Following Modifications:

After writing the file to disk, the input file should be read into an array, sorted using the static Array.sort() method from the Java API and then displayed in the terminal window with each of the numbers on a separate line (maximum grade of 8 points if the sort is missing)

Sorting of arrays was covered in CST112 and in Chapter 6 of the textbook; you will receive up to 9 out of ten (10) points for this assignment if you read the input file into an array of 100 elements and sort the array using either a selection or insertion sort before writing the list of numbers to the terminal window

The maximum number of points for this assignment is out of ten (10) if you simply write the input file to the terminal window without sorting the list

Explanation / Answer

The Read/Write data from a file can be done in many different ways and each method has its own logic and rules.

Let me give you an overview about how to Write the files and all. The FileWriter class is used to substitue the writing method within a file to access the data and modify it accordingly.

FileWriter fwOutput = new FileWriter("Exercise12_15.txt");

Next, The second important part of files scenario is to reading the data from a file. It can be used as

FileReader fwIntput = new FileReader("Exercise12_15.txt");

I have attached the code for generating the 100 integers into the file.

Code:-

import java.io.*;
import java.util.Random;

public class ReadWriteData{
public static void main(String[] args) throws IOException{

FileWriter fwOutput = new FileWriter("Exercise12_15.txt");

BufferedWriter out = new BufferedWriter(fwOutput);

Random rand = new Random();

for (int x = 1; x <= 100; ++x){

int randomValues = rand.nextInt(100);

out.write(randomValues+" ");

}
out.close();

FileReader fwIntput = new FileReader("Exercise12_15.txt");

BufferedReader in = new BufferedReader(fwIntput);

String rowPerLines = in.readLine();

while (rowPerLines != null){

System.out.println(rowPerLines);

rowPerLines = in.readLine();

}

in.close();

}
}