Question: Complete a partially implemented Java program Write a method that takes arguments and returns no value Use a FileInputStream to open a file for reading
Complete a partially implemented Java program
Write a method that takes arguments and returns no value
Use a FileInputStream to open a file for reading
Use a Scanner to read input from a file
Use a FileOutputStream to open a file for writing
Use a PrintWriter to write to a file
Use the Scanner hasNextLine method to determine if there is more data in a file to read
Flush and close a file
Read and analyze a Java program
Problem Description and Given Info
You must complete the makeCopy method in the Java program you are given below. Fill in the missing pieces described in the comments. This program will copy the contents of one file into another file, line by line.
You must write a loop to iterate while the scanner has a next line.
Within the loop you must:
use the scanner to read the next line from the input file and store it in a String variable.
use the file writer to write the String to the output file.
After the loop you must:
flush the PrintWriter
close the PrintWriter
make no other changes to the code.
Be sure that you carefully read the code and understand how it works.
This program will copy the contents of one file into another file,
line by line.
You must complete the makeCopy method and fill in the missing pieces described in the code below.
you must write a loop to iterate while the scanner has a next line.
within the loop you must
use the scanner to read the next line from the input file and store it in a String variable.
use the file writer to write the String to the output file.
after the loop you must
flush the PrintWriter
close the PrintWriter
make no other changes to the code,
import java.ioFileInputStream;
import java.ioFileOutputStream;
import java.ioPrintWriter ;
import java.ioIOException;
import java.util.Scanner;
class Main
public static void mainString args throws IOException
makeCopyoriginaldat", "copy.dat";
print out the contents of the copy to
verify that the original was copied correctly
printFilecopydat";
public static void makeCopyString inputFileName, String outputFileName throws IOException
open a File with the given inputFileName
and instantiate a Scanner object to read the File
Scanner myFileReader new Scannernew FileInputStreaminputFileName;
open a File with the given outputFileName
and instantiate a PrintWriter object to write to the File
PrintWriter myFileWriter new PrintWriternew FileOutputStreamoutputFileName;
loop while the Scanner has a next line
read the next line from the Scanner and store it in a String variable
write the line to the output file
loop ends
flush the PrintWriter
close the PrintWriter
myFileReader.close;
make no changes to this printFile method
public static void printFileString fileName throws IOException
Scanner myFileReader new Scannernew FileInputStreamfileName;
while myFileReaderhasNextLine
System.out.printlnmyFileReadernextLine;
myFileReader.close;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
