That file also accompanies this document on Blackboard. Name your program ReadSo
ID: 3815625 • Letter: T
Question
That file also accompanies this document on Blackboard. Name your program ReadSoap.java and make sure the program includes ALL of the following:
1. A prompt for the user to input the file name to be read (i.e. soap.txt)
2. A check for the file’s existence prior to attempting to read lines in.
3. The appropriate construct so that the program continues to read lines until there are NO MORE lines found in the file.
4. A display of each line read on the console screen.
5. A file CLOSE at the end of the program code.
THIS IS THE CODE:
SHOULD READ THIS OUTPUT:
Explanation / Answer
# SORRY, THE IMPORT HAS BEEN TYPED TWICE BY MISTAKE
HERE IS THE CORRECTED CODE BELOW:
import java.util.Scanner;
import java.io.*;
public class Readsoap
{
public static void main(String[] args) throws IOException
{
Scanner keyboard = new Scanner(System.in);
String fileName = keyboard.nextLine();
String line = null;
// Code here to get the file name from the user.
File file = new File(fileName);
if(file.exists() && !file.isDirectory()) {
/* FileReader reads text files in the default encoding */
FileReader fileReader = new FileReader(fileName);
/* wrap the FileReader in BufferedReader */
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null)
{
System.out.println(line);
}
/* close the file after use */
bufferedReader.close();
}
else{
System.out.println("Error reading file '" + fileName + "'");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.