In: Computer Science
Write a complete Java program that does the following:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class ReadWriteNumbers {
public static void main(String[] args) {
final int MAX = 100;
//creating array with size 100
int arr[] = new int[MAX];
//opening the file to read data
Scanner sc=null;
try{
sc = new Scanner(new File("data.txt"));
}
catch (Exception e) {
System.out.println("data.txt does not exist");
return;
}
int index = 0;
// looping through file and reading upto 100 integers
while (sc.hasNextInt() && index < 100) {
arr[index] = sc.nextInt();
index++;
}
// opening file to write the data
PrintWriter pw=null;
try {
pw = new PrintWriter(new File("result.txt"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// iterating array from backside to write results in reverse order
for (int i = index - 1; i >= 0; i--)
pw.println(arr[i]);
pw.close();
System.out.println("Succes...");
}
}
NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.
Please Like and Support me as it helps me a lot