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

 /*
Write a java program to reverse the content of file using RandomAccessFile.
*/
import java.io.*;
import java.util.*;
class FileReverse
{
File f,f2;
String str;
FileReverse()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the source path: ");
String path=s.nextLine();
System.out.println("Enter the source name: ");
String name=s.nextLine();
f=new File(path,name);
}
void reverse()
{
try
{
RandomAccessFile file = new RandomAccessFile(f,"r");
long n= f.length()-1;
int i=0;
while (n>=0)
{
if (n==-1)
break;
else
{
file.seek(n);
i= file.read();
n=n-1;
System.out.print((char)i);
}
}
file.close();
}catch(Exception e)
{
System.out.print(e);
}
}
}
class TestFileReverse
{
public static void main(String args[])
{
FileReverse obj=new FileReverse();
obj.reverse();
}
}

0 Comments