Thursday, 12 April 2018

Corejava-Slip9

Slip 9. Write a java program to display the contents of a file in reverse order.

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Main
{
public static void main(String args[])throws Exception,IOException
{
Scanner scanner=new Scanner(new File("a.txt")).useDelimiter("\\z");
String contents=scanner.next();
contents=new StringBuffer(contents).reverse().toString();
System.out.println("Reversed String:"+contents);
FileWriter fstream=new FileWriter("a.txt");
BufferedWriter out=new BufferedWriter(fstream);
out.write(contents);
out.close();
}
}