Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Write a program that reads each line in a file, reverses its lines, and writes them to another file. For
example, if the file input.txt contains the lines
------------------------------
Mary had a little lamb
Its fleece was white as snow
And everywhere that Mary went
The lamb was sure to go.
-----------------------
and you run reverse input.txt output.txt then output.txt contains

The lamb was sure to go.
And everywhere that Mary went
Its fleece was white as snow
Mary had a little lamb

What I have tried:

Java
package a3;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.ArrayList;
public class A3 {

    
    public static void main(String[] args) throws IOException {
     FileWriter input =new FileWriter("input.txt");
     FileWriter output =new FileWriter("output.txt");
        
        try{
                input.write("Mary had a little lamb /n");
              
                input.write("Its fleece was white as snow ./n");
                
                input.write("And everywhere that Mary went . /n");
              
                input.write("The lamb was sure to go. /n");
               
                input.close();
            Scanner sc=new Scanner((Readable) input);
           while(sc.hasNextLine()){
           System.out.println(sc.next());
           } 
          
               output.write(sc.nextLine());
        
        
        
        
        
        }
            catch (IOException e) {
            }
       
       
    }
       
      
    }
Posted
Updated 22-Nov-22 5:25am
v2

If you look in the documentation, you will find that Java contains a Fiules.ReadAllLines method: Files (Java Platform SE 8 )[^]
This returns a List of strings - which is really easy to output backwards ...
 
Share this answer
 
Comments
CPallini 22-Nov-22 11:30am    
Well, that's smarter than my approach.
5.
You might try the following approach (filling the gaps, see the TODO remarks)
Java
package a3;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.ArrayList;
public class A3
{
  public static void main(String[] args) throws IOException
  {
    {
      FileWriter input = new FileWriter("input.txt");
      input.write("Mary had a little lamb \n");
      input.write("Its fleece was white as snow. \n");
      input.write("And everywhere that Mary went. \n");
      input.write("The lamb was sure to go. \n");
      input.close();
    }
    {
      FileReader input = new FileReader("input.txt");
      Scanner sc = new Scanner(input);
      //TODO: create the ArrayList
      while ( sc.hasNextLine() )
      {
        System.out.println(sc.nextLine());
        //TODO: append the line to the ArrayList
      }
      //TODO: open the output file
      //TODO: iterate backward the ArrayList, writing the lines to the output file
      //TODO: close the output file
    }
  }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900