package randomAccess;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* Class to load a set of employee data to a file.
*
* @author Bryan J. Higgs, 31 January, 2001.
*/
public class EmployeeDataLoader
{
/**
* Main entry point.
*
* @param args command line arguments.
* args[0] is the name of the file to load.
*/
public static void main(String[] args)
{
if (args.length < 1)
{
System.err.println(
"Usage: java randomAccess.EmployeeDataLoader <file-to-load>");
return;
}
// Extract the filename
String fileName = args[0];
// Construct all employees (empID, empName, empSalary, managedBy)
Employee bigBoss = new Employee(1, "Richie Rich", 2000000.0, -1);
Employee cfo = new Employee(2, "Guy Noire", 200000.0, 1);
Employee hrBoss = new Employee(3, "Prescott Hireling", 100000.0, 1);
Employee sectry = new Employee(4, "Dottie Magnusson", 40000.0, 3);
Employee opsBoss = new Employee(5, "Frank Getitdone", 120000.0, 1);
Employee minion = new Employee(6, "Joe Duzit", 30000.0, 5);
RandomAccessFile file = null;
try
{
// Create random access file object for writing.
file = new RandomAccessFile(fileName, "rw");
// Write all employees to the file
// NOTE: The employee ID is the index of the employee data,
// so the employees are written in employee ID order.
bigBoss.write(file);
cfo.write(file);
hrBoss.write(file);
sectry.write(file);
opsBoss.write(file);
minion.write(file);
}
catch (IOException ex)
{
ex.printStackTrace();
}
finally
{
// Close the file, under all circumstances.
if (file != null)
{
try
{
file.close();
}
catch (IOException ex)
{
// Ignore
}
}
}
}
} |