Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I have a task to merge 2 java file applications to be in only one java application; these are the requirements. Merage the create

Hello, I have a task to merge 2 java file applications to be in only one java application; these are the requirements.

Merage the create and read sequential file applications in one application, and you can use

1- Create a file for update(open in Append mode)

2- read

3- for exit

this is the read sequential file code

import java.io.EOFException; import java.io.IOException; import java.io.ObjectInputStream; import java.nio.file.Files; import java.nio.file.Paths; public class ReadSequentialFile { private static ObjectInputStream input; public static void main(String[] args) { openFile(); readRecords(); closeFile(); } // enable user to select file to open public static void openFile() { try // open file { input = new ObjectInputStream( Files.newInputStream(Paths.get("clients.ser"))); } catch (IOException ioException) { System.err.println("Error opening file."); System.exit(1); } } // read record from file public static void readRecords() { System.out.printf("%-10s%-12s%-12s%10s%n", "Account", "First Name", "Last Name", "Balance"); try { while (true) // loop until there is an EOFException { Account record = (Account) input.readObject(); // display record contents System.out.printf("%-10d%-12s%-12s%10.2f%n", record.getAccount(), record.getFirstName(), record.getLastName(), record.getBalance()); } } catch (EOFException endOfFileException) { System.out.printf("%nNo more records%n"); } catch (ClassNotFoundException classNotFoundException) { System.err.println("Invalid object type. Terminating."); } catch (IOException ioException) { System.err.println("Error reading from file. Terminating."); } } // end method readRecords // close file and terminate application public static void closeFile() { try { if (input != null) input.close(); } catch (IOException ioException) { System.err.println("Error closing file. Terminating."); System.exit(1); } } 

-------------------------------------------------------------------------------------------------------------------------------------------------

And this is the create a sequential file code.

import java.io.IOException; import java.io.ObjectOutputStream; import java.nio.file.Files; import java.nio.file.Paths; import java.util.NoSuchElementException; import java.util.Scanner; public class CreateSequentialFile { private static ObjectOutputStream output; // outputs data to file public static void main(String[] args) { openFile(); addRecords(); closeFile(); } // open file clients.ser public static void openFile() { try { output = new ObjectOutputStream( Files.newOutputStream(Paths.get("clients.ser"))); } catch (IOException ioException) { System.err.println("Error opening file. Terminating."); System.exit(1); // terminate the program } } // add records to file public static void addRecords() { Scanner input = new Scanner(System.in); System.out.printf("%s%n%s%n? ", "Enter account number, first name, last name and balance.", "Enter end-of-file indicator to end input."); while (input.hasNext()) // loop until end-of-file indicator { try { // create new record; this example assumes valid input Account record = new Account(input.nextInt(), input.next(), input.next(), input.nextDouble()); // serialize record object into file output.writeObject(record); } catch (NoSuchElementException elementException) { System.err.println("Invalid input. Please try again."); input.nextLine(); // discard input so user can try again } catch (IOException ioException) { System.err.println("Error writing to file. Terminating."); break; } System.out.print("? "); } } // close file and terminate application public static void closeFile() { try { if (output != null) output.close(); } catch (IOException ioException) { System.err.println("Error closing file. Terminating."); } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Seven Databases In Seven Weeks A Guide To Modern Databases And The NoSQL Movement

Authors: Luc Perkins, Eric Redmond, Jim Wilson

2nd Edition

1680502530, 978-1680502534

More Books

Students also viewed these Databases questions