I need to import a text file and export a text file with the lines in the opposite order
Example input:
abc
123
First line
Expected output:
First line
123
abc
This is what I have so far. It reverses the lines but not the line order. Any help would be appreciated
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class reversetext {
public static void main(String[] args) throws IOException {
try {
File sourceFile = new File("in.txt");//input File Path
File outFile = new File("out.txt");//out put file path
Scanner content = new Scanner(sourceFile);
PrintWriter pwriter = new PrintWriter(outFile);
while(content.hasNextLine()) {
String s = content.nextLine();
StringBuffer buffer = new StringBuffer(s);
buffer = buffer.reverse();
String rs = buffer.toString();
pwriter.println(rs);
}
content.close();
pwriter.close();
}
catch(Exception e) {
System.out.println("Something went wrong");
}
}
}