Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Want solutions in Netbeans This lesson will demonstrate the use of the Path statement for writing data into a text file and for reading the

Want solutions in Netbeans

This lesson will demonstrate the use of the Path statement for writing data into a text file and for reading the data back out of the same text file. The write file is slightly different than the last one as it does not incorporate the use of a GUI for input. But you do know how to convert this to a GUI and it would be very good practice for you to do so. Tis code will you the CMD window if you elect to run it in that window but you should place it into your NetBeans project folder. At a minimum I would like you to color the output in NetBeans. At a maximum I want you to convert the input code to a GUI input. It will lend itself to good practice. Make sure that you customize the statement below

Paths.get("C:\\users\\Bryan\\javaHwrk\\Employees.txt");

to match the path YOU want to use on your computer. The path above is only for use on MY computer. You have to determine where you want the 'write' program to to store the data. Also be aware that thge double back slashes are not a typo. They are required for a unicode path structure.

==================================================================================================================================

The WriteEmployeeFile Code: Copy this code into NetBeans and run it for yourself. Save it as WriteEmployeeFile

import java.nio.file.*; import java.io.*; import static java.nio.file.StandardOpenOption.*; import java.util.Scanner; public class WriteEmployeeFile { public static void main(String[] args) { Scanner input = new Scanner(System.in); Path file = Paths.get("C:\\users\\Bryan\\javaHwrk\\Employees.txt"); // <-- customize this to your own path String s = ""; String delimiter = ","; int id; String name; double payRate; final int QUIT = 999; try { OutputStream output = new BufferedOutputStream(Files.newOutputStream(file, CREATE)); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output)); System.out.print("Enter employee ID number >> "); id = input.nextInt(); while(id != QUIT) { System.out.print("Enter name for employee #" + id + " >> "); input.nextLine(); name = input.nextLine(); System.out.print("Enter pay rate >> "); payRate = input.nextDouble(); s = id + delimiter + name + delimiter + payRate; writer.write(s, 0, s.length()); writer.newLine(); System.out.print("Enter next ID number or " + QUIT + " to quit >> "); id = input.nextInt(); } writer.close(); } catch(Exception e) { System.out.println("Message: " + e); } } }

The ReadEmployeeFile Code: Copy this code into NetBeans and run it for yourself. Save it as ReadEmployeeFile

Notice that the path name is exactly the same as the write file. It is not imperative that you keep the file in the same location for the read program. You can move the data file to a new location if you feel that is prudent but you will have to modify the 'Read' path to accomodate the change.

Remember that at a minimum I want this output to be in color and just for parctice I want some colors defined in standard style (blue) and some in hexidecimal style. I also want the 'write' file to be converted to a GUI input.

import java.nio.file.*; import java.io.*; public class ReadEmployeeFile { public static void main(String[] args) { Path file = Paths.get("C:\\users\\Bryan\\javaHwrk\\Employees.txt"); String[] array = new String[3]; String s = ""; String delimiter = ","; int id; String name; double payRate; double gross; final double HRS_IN_WEEK = 40; double total = 0; try { InputStream input = new BufferedInputStream(Files.newInputStream(file)); BufferedReader reader = new BufferedReader(new InputStreamReader(input)); System.out.println(); s = reader.readLine(); while(s != null) { array = s.split(delimiter); id = Integer.parseInt(array[0]); name = array[1]; payRate = Double.parseDouble(array[2]); gross = payRate * HRS_IN_WEEK; System.out.println("ID#" + id + " " + name + " $" + payRate + " $" + gross); total += gross; s = reader.readLine(); } reader.close(); } catch(Exception e) { System.out.println("Message: " + e); } System.out.println(" Total gross payroll is $" + total+" "); } }

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

Building Database Driven Catalogs

Authors: Sherif Danish

1st Edition

0070153078, 978-0070153073

Students also viewed these Databases questions

Question

How can user view performance model of KNN model?

Answered: 1 week ago

Question

What is a P&ID? I am not satisfy give downvote

Answered: 1 week ago

Question

7. It is advisable to do favors for people whenever possible.

Answered: 1 week ago