Question: String fileName is read from input. The try block opens a file named fileName for output. Three integers are read from input, and each integer

String fileName is read from input. The try block opens a file named fileName for output. Three integers are read from input, and each integer is written into the file. Write the finally block to perform the following tasks, if the output file is open:
Write to the file numValuesRead, followed by " value(s) read" and a newline.
If numValuesRead is greater than zero, write to the file "Last value is: ", followed by chiveLast and a newline.
Close the file. import java.util.Scanner;
import java.io.FileOutputStream;
import java.io.PrintWriter;
public class WriteLastChiveFile {
public static void main(String[] args){
Scanner scnr = new Scanner(System.in);
PrintWriter chiveInWriter = null;
String fileName;
int chiveValue;
int numValuesRead =0;
int chiveLast =0;
fileName = scnr.next();
try {
chiveInWriter = new PrintWriter(new FileOutputStream(fileName));
for (numValuesRead =0; numValuesRead <3; ++numValuesRead){
chiveValue = scnr.nextInt();
chiveInWriter.println(chiveValue);
chiveLast = chiveValue;
}
}
catch (Exception e){
System.out.println("Error!");
}
/* Your code goes here */
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!