Examlex

Solved

Import Java.nio.file.*; Import Java.io.*;

question 62

Essay

import java.nio.file.*;
import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;
import static java.nio.file.StandardOpenOption.*;
import java.util.Scanner;
public class CreateEmployeesRandomFile
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
Path file =
Paths.get("C: \\Java\\Chapter.13\\RandomEmployees.txt");
String s = "000, ,00.00" +
System.getProperty("line.separator");
final int RECSIZE = s.length();
FileChannel fc = null;
String delimiter = ",";
String idString;
int id;
String name;
String payRate;
final String QUIT = "999";
try
{
fc = (FileChannel)Files.newByteChannel(file, READ, WRITE);
System.out.print("Enter employee ID number >> ");
idString = input.nextLine();
while(!(idString.equals(QUIT)))
{
_____________________________
System.out.print("Enter name for employee #" +
id + " >> ");
name = input.nextLine();
System.out.print("Enter pay rate >> ");
payRate = input.nextLine();
s = idString + delimiter + name + delimiter +
payRate + System.getProperty("line.separator");
byte[] data = s.getBytes();
ByteBuffer buffer = ByteBuffer.wrap(data);
_____________________________
fc.write(buffer);
System.out.print("Enter next ID number or " +
QUIT + " to quit >> ");
idString = input.nextLine();
}
fc.close();
}
catch (Exception e)
{
System.out.println("Error message: " + e);
}
The above program will accept any number of employee records as user input and write the records to a file in a loop. In the first shaded line, create the statement to accept the employee data value from the keyboard as a String and convert it to an integer using the parseInt() method. In the second shaded line, create the statement to compute the record's desired position by multiplying the ID number value by the record size.


Definitions:

Evolutionary Relationships

The connections among species or groups of organisms that show how they are related through evolution, often depicted in a phylogenetic tree.

Organisms

Living entities that can function independently, ranging from microscopic bacteria to complex animals and plants.

Aristotle

An ancient Greek philosopher and polymath, whose writings covered diverse fields such as metaphysics, biology, ethics, and politics, significantly influencing Western intellectual history.

Phylogeny

The complete evolutionary history of a group of organisms.

Related Questions