4-11 FileReverse

Post date: Apr 2, 2014 6:45:35 PM

/* Program to Reverse the Contents of a File and Print it in another file */

package filereverse;

import java.io.*;

import java.util.Scanner;

public class FileReverse 

{

    public static void main(String[] args)throws IOException 

    {

           Scanner scan=new Scanner(System.in);

           System.out.print("Enter Your File Path : ");

           File f=new File(scan.next());

           Scanner input=new Scanner(f);

           System.out.print("Enter Your File Reverse Path : ");

           PrintWriter output=new PrintWriter(scan.next());

           int lines=0,l=1;

            while(input.hasNextLine())

                {      

                    lines++;

                   input.nextLine();

                }

            input=new Scanner(f);

            while(input.hasNextLine())

                {  

                    String s1=input.nextLine();

                    if(l==lines)

                    {

                        output.println(s1);

                        l=0;

                        lines--;

                        input=new Scanner(f);

                    }

                    l++;         

                }

            input.close();

            output.close();

        

    }   

}