Answered step by step
Verified Expert Solution
Question
1 Approved Answer
please help with java assingment. my code is below - i ' m unsure how to write the reversed contents into a new file -
please help with java assingment. my code is below im unsure how to write the reversed contents into a new file please help, my code is below: package assignment;
import java.ioFile;
import java.ioFileWriter;
import java.ioIOException;
import java.util.ArrayList;
import java.util.Scanner;
public class Reverse
public static void mainString args throws IOException
ArrayList lines new ArrayList;
Read and process command line arguments
int files ;num of command line args that are files
String inFile ;
String outFile ;
if argslength
usage;
System.exit;
else
Read the command line arguments
for int i ; i args.length; i
String arg argsi;
if CharacterisDigitargcharAti
usage;
return;
else
it is a file name
files;
if files
inFilearg;
else if files
outFile arg;
construct the scanner and print writer onjects for the reading and writing
File inputFile new FileinFile;
open file, reverse each line, and add it to the array list
tryScanner in new ScannerinputFile
while inhasNextLine
String line innextLine;
String reversed reverseStringline; calling a method
lines.addreversed;
write the output to the txxt file
tryFileWriter out new FileWriteroutFile
lines.forEachline
try
out.writeline;
catch IOException e
eprintStackTrace;
;
here
method reverses a string
static String reverseStringString r
String s ;
for int irlength; i; i
char c rcharAti;
s sc;
return s;
static void usage
System.out.printlnUsage: java reverse infile outfile";
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