Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA Create a PrintFile class that reads a files lines into a List < String > using an ArrayList < String > and then prints

JAVA Create a PrintFile class that reads a files lines into a List< String > using an ArrayList< String > and then prints them out:

Modify main so that it only expects one command line argument, and if it gets a different number prints out this usage message: Usage: PrintFile filename

Remove all of the logic related to the output file, its not needed in PrintFile

Create a List< String > with a variable called list and instantiate it with an ArrayList< String >

In the while loop use add to add the files lines to list like in the ArrayListDemo program

After closing the input file, print the number of lines in the file, and then use a for-each loop to get them from list and print them out, one per line; the line saying how many lines are in the file should look like this:

There are n lines in the file: // n is the size of the list after the while loop 

Hints: you can either count the number of lines as you are reading them in or, after the while loop, the method list.size() returns the number of items in list

Here is a template that's given:

import java.io.*;

public class PrintFile // this is CopyFile.java in the Sakai Week 12 Source Code folder { public static void main(String[] args) // uses program command line arguments { /* as mentioned above, change this so the program only expects 1 command line argument, and if that's not the case it prints Usage: PrintFile filename */ if (args.length != 2) { // change this if condition System.out.println("Usage: CopyFile inputFile outputFile"); // change this error message System.exit(0); } Scanner170 inStream = IO.inFile(args[0]); if (inStream == IO.inError) { System.out.println("Error: could not read input file " + args[0]); System.exit(0); } /* as mentioned above, remove this output stream processing section, it's not needed for PrintFile: */ PrintStream outStream = IO.newFile(args[1]); // assure output doesn't exist yet if (outStream == IO.outError) { System.out.println("Error: could not create output file " + args[1]); System.exit(0); } // could also use IO.exists(args[1]) first to check if output file exists

/* here is where to add code to create a List with a variable list and instantiate it with an ArrayList - add one line of code here: */ /* as mentioned above, modify the while loop body so that it uses List's add method to add each line from the input file to list: */ while (inStream.hasNextLine()) outStream.println(inStream.nextLine()); // replace this line to use List's add

inStream.close(); // close the input file when done reading from it /* remove the following line since there is no longer an output file stream: */ outStream.close(); // close the output file when done writing to it /* here is where you print out There are n lines in the file: - use list.size() to determine how many lines there are in the file (the number put in the list): */ /* finally, write a for-each loop here to pull out each String that is in list and print it to the monitor: */ } }

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_2

Step: 3

blur-text-image_3

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 Processing

Authors: David J. Auer David M. Kroenke

13th Edition

B01366W6DS, 978-0133058352

More Books

Students also viewed these Databases questions

Question

What is the difference between Needs and GAP Analyses?

Answered: 1 week ago

Question

What are ERP suites? Are HCMSs part of ERPs?

Answered: 1 week ago