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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started