QuestionQuestion

Sequential File I/O: You will create, write to and read from a sequential-access file using the Formatter and Scanner class. The file will contain records of students with fields consisting of the student ID (type int) and the student’s last name (String) and the student’s first name (String).
You must also handle the exceptions below.
• SecurityException: If you don’t have permission to create, read, or write to a file, an exception will be thrown.
• FileNotFoundException: File doesn’t exist AND you don’t have permission to create a new file.
• NoSuchElementException: Data is in the wrong format.
• FormatterClosedException: If the Formatter object is closed.

Your program should:
• Allow the user to enter the directory path to the file.
• Use the try with resources for the Formatter and Scanner objects.
• Allow the user to enter as many records (Student ID, last name and first name) as they want.
• Handle the exceptions listed above separately so that you can print out a descriptive prompt to the user if the exception is thrown.
• Add code to read and print the file contents in a nicely formatted statement that describes each field in the student records to the user.
• Display the size of the file in bytes with a description using one of the methods in the chapter.
• Display the string name of the file using one of the methods in the chapter.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Formatter;
import java.util.FormatterClosedException;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class Assignment10 {

    public static void main(String[] args) {
       // Read in the directory path from the user 4
       Scanner keyboard = new Scanner(System.in);
       System.out.println("Enter directory path to file:");

       // create Path object based on user input
       Path path = Paths.get(keyboard.nextLine());

       // create output file name
       String fileName = path.toAbsolutePath().toString();

       //Create a Formatter object using “try with resources” at the location the user entered 4
       //Handle each exception making sure to place the error-handling code around the code that could throw that error and to describe the exception thrown and what it means 8            
       try (Formatter output = new Formatter(fileName)) {

            // • Allow the user to enter as many records (Student ID, last name and first name) as they want.
            System.out.printf("%s%n",
                   "To terminate keyboard, type a letter, then press enter");...

By purchasing this solution you'll be able to access the following files:
Solution.java.

$50.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats