Question: how do i modify the code to print the lines in reverse order to a file called reverse.txt? I'm having trouble figuring out how to

how do i modify the code to print the lines in reverse order to a file called reverse.txt? I'm having trouble figuring out how to reverse the lines from the original file. I tried using an array but i dont know if im doing it right.

haikuFun.txt file:

Im writing my code, But I do not realize, Hours have gone by. Type, type, type away Compile. Run. Hip hip hooray! No error today!

My Code:

import java.util.*; import java.io.*;

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

FileInputStream fileIn = new FileInputStream("haikuFun.txt"); Scanner stdin = new Scanner(fileIn); String[] lines = new String[6]; lines[0] = stdin.nextLine(); lines[1] = stdin.nextLine(); lines[2] = stdin.nextLine(); lines[3] = stdin.nextLine(); lines[4] = stdin.nextLine(); lines[5] = stdin.nextLine(); for (int i = 0; i < lines.length; i++) { System.out.println(lines[i]); } fileIn.close(); FileOutputStream fileOut = new FileOutputStream("reverse.txt"); PrintWriter out = new PrintWriter(fileOut); for (int i = 0; i < lines.length; i--) { System.out.println(lines[i]); } out.flush(); fileOut.close(); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!