Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Application Development And Design

Authors: Michael V. Mannino

1st Edition

0072463678, 978-0072463675

More Books

Students also viewed these Databases questions

Question

discuss the arguments for and against CSR programmes

Answered: 1 week ago

Question

What is DDL?

Answered: 1 week ago