Here is my code - I am trying to reverse the contents of a file by reading the file through an array.
Is there a more efficient and quicker way of achieving this? I feel this is too much for this type of question?
class ReverseNumbers {
public static void main(String[] args) throws FileNotFoundException {
File file = new File("numbers.txt");
Scanner Scan = new Scanner(file);
int numoflines = 0;
while(Scan.hasNextLine()) {
Scan.nextLine();
numoflines++;
}
Scan.close();
Scan = new Scanner(file);
int buffer[] = new int[numoflines]; //size
for(int i=0; i<buffer.length; i++) {
buffer[i] = Scan.nextInt();
}
for(int i=buffer.length-1; i>=0; i--) {
int reverse = buffer[i];
System.out.println(reverse);
}
}
}