Please help I am stuck. I am trying to retrieved text from a file named "text.txt" and reversing the contents to another file named "output". Then i have to display the contents of "output" to the console. I am able to display the reversed text to console but only from the original file.
import java.io.*;
public class ReverseFile {
public static void main(String[] args) {
try {
File file = new File("text.txt");
FileReader reader = new FileReader(file);
char[] Xnew = new char[(int)file.length()];
char[] x = new char[(int)file.length()];
StringBuffer Xbuf = new StringBuffer();
reader.read(x,0,(int)file.length());
int leng = (int)file.length();
for (int i = 0, z = leng - 1; i < leng ; i++, z--) {
Xnew[i] = x[z];
Xbuf.append(Xnew[i]);
}
System.out.println("--------Text before being reverse--------");
System.out.println(x);
System.out.println("--------Text after being reversed--------");
System.out.println(Xbuf.toString());
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}