i have to reverse a passage of text in java. For example if the text file had
Hello world
This is a example
and you run java reverse input.txt output.txt, then output.txt contains
This is a example
Hello world
the code I have so far only reads the input. txt in the standard way. Any ideas on how to reverse. Im stuck
import java.io.*;
public class Test {
public static void main(String [] args) {
/**
* The name of the file and the location of the file.
*/
String fileName = "test.txt";
/**
* This will enter one line at a time
* FileReader reads text files
*/
String line = null;
try {
FileReader fileReader =
new FileReader(fileName);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader =
new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
/**
* Closing the file.
*/
bufferedReader.close();
}
/**
*Telling the program what message to show if the file is not found
*/
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
}
}
}