📄 demomergeupdate.java
字号:
package fileMerging;
import java.io.*;
import java.util.StringTokenizer;
import dslib.file.ObjectFileUos;
/** This class does merge-updates on a binary master file from text transaction files. */
public class DemoMergeUpdate
{
/** Perform an initial merge update just to add employees to an initial empty master file,
then apply a second merge-update to apply transactions to the resulting master file. */
public DemoMergeUpdate() throws Exception
{
PrintWriter outFile = new PrintWriter(new FileWriter("./fileMerging/outputMergeUpdate.txt"));
mergeUpdate("firstMaster","firstTrans","secondMaster", "error1.log");
outFile.println("After initial adds...");
transferRecordFile(outFile, "secondMaster");
transferErrorFile(outFile, "error1.log");
mergeUpdate("secondMaster","secondTrans","thirdMaster", "error2.log");
outFile.println("\nAfter updates, adds and deletes...");
transferRecordFile(outFile, "thirdMaster");
transferErrorFile(outFile, "error2.log");
outFile.close();
}
KeyedEmployee curEmployee, transEmployee;
boolean allMasterRecordsProcessed, allTransactionsProcessed;
String transType;
/** Apply the transactions read from the transName file to the masterName file,
and produce a newMasterName file. <br>
Analysis: Time = O(m + t) file reads and writes, where m is the size of the old master file <br>
and t is the size of the transaction file. */
void mergeUpdate(String masterName, String transName,
String newMasterName, String errorFileName) throws Exception
{
ObjectFileUos masterFile = new ObjectFileUos(masterName, "r");
BufferedReader transFile = new BufferedReader(new FileReader(transName));
ObjectFileUos newMasterFile = new ObjectFileUos(newMasterName, "rw");
newMasterFile.wipeOut();
PrintWriter errorFile = new PrintWriter(new FileWriter(errorFileName));
readEmployeeRecord(masterFile);
readTransaction(transFile);
while (!allTransactionsProcessed & !allMasterRecordsProcessed)
{
if (curEmployee.key().compareTo(transEmployee.key()) < 0)
{
newMasterFile.writeObject(curEmployee);
readEmployeeRecord(masterFile);
}
else if (curEmployee.key().compareTo(transEmployee.key()) == 0)
{
if (transType.equals("Replace"))
{
newMasterFile.writeObject(transEmployee);
readTransaction(transFile);
readEmployeeRecord(masterFile);
}
else if (transType.equals("Add"))
{
errorFile.println(addErrorMessage(transEmployee.key()));
readTransaction(transFile);
}
else // transType.equals("Delete")
{
readTransaction(transFile);
readEmployeeRecord(masterFile);
}
}
else // curEmployee.key().compareTo(transEmployee.key()) > 0
{
if (transType.equals("Replace"))
errorFile.println(replaceErrorMessage(transEmployee.key()));
else if (transType.equals("Add"))
newMasterFile.writeObject(transEmployee);
else // transType.equals("Delete")
errorFile.println(deleteErrorMessage(transEmployee.key()));
readTransaction(transFile);
}
}
/* Dump the remaining employees to the newMasterFile. */
while (!allMasterRecordsProcessed)
{
newMasterFile.writeObject(curEmployee);
readEmployeeRecord(masterFile);
}
/* Process any remaining transactions. */
while (!allTransactionsProcessed)
{
if (transType.equals("Replace"))
errorFile.println(replaceErrorMessage(transEmployee.key()));
else if (transType.equals("Add"))
newMasterFile.writeObject(transEmployee);
else // transType.equals("Delete")
errorFile.println(deleteErrorMessage(transEmployee.key()));
readTransaction(transFile);
}
transFile.close();
masterFile.close();
System.out.println("The contents of the new master file is\n" + newMasterFile.toString());
newMasterFile.close();
errorFile.close();
}
/** Read in an employee from employeeFile. <br>
Analysis: Time = 1 file read */
void readEmployeeRecord (ObjectFileUos employeeFile)
{
if (!employeeFile.eof())
{
allMasterRecordsProcessed = false;
curEmployee = (KeyedEmployee) employeeFile.readObject();
}
else
{
allMasterRecordsProcessed = true;
curEmployee = null;
}
}
/** Read in a transaction type and a transaction employee from transFile.
Analysis: Time = 4 file reads */
void readTransaction(BufferedReader transFile) throws Exception
{
transType = transFile.readLine();
if (transType == null)
{
allTransactionsProcessed = true;
transEmployee = null;
}
else
{
allTransactionsProcessed = false;
String empName = transFile.readLine();
int empNumber = Integer.parseInt(transFile.readLine());
float empRate = Float.parseFloat(transFile.readLine());
String empClass = transFile.readLine();
transEmployee = new KeyedEmployee(empName, empNumber, empRate, empClass);
}
}
/** Use the PrintWriter to place in a text file the Objects of binary file 'name'.
Analysis: Time = O(n) file reads, n = size of the binary file */
void transferRecordFile(PrintWriter outFile, String name)
{
ObjectFileUos objectFile = new ObjectFileUos(name, "r");
while (!objectFile.eof())
{
Object obj = objectFile.readObject();
outFile.println(obj);
}
objectFile.close();
}
/** Use the PrintWriter to place in a text file the error messages from text file 'name'.
Analysis: Time = O(n) file reads, n = number of lines in the error text file */
void transferErrorFile(PrintWriter outFile, String name) throws Exception
{
BufferedReader sourceFile = new BufferedReader(new FileReader(name));
outFile.println("\nErrors generated:");
String lineRead = sourceFile.readLine();
while (lineRead != null)
{
outFile.println(lineRead);
lineRead = sourceFile.readLine();
}
sourceFile.close();
}
/** Return the error message for when an employee is to be replaced that does not exist.
Analysis: Time = O(1) */
String replaceErrorMessage(Comparable employeeNumber)
{
return "\nReplace error encountered.\nAn employee with number "
+ employeeNumber + " was to be replaced, but no such employee was found.";
}
/** Return the error message for when an employee is to be deleted that does not exist.
Analysis: Time = O(1) */
String deleteErrorMessage(Comparable employeeNumber)
{
return "\nDelete error encountered.\nAn employee with number "
+ employeeNumber + " was to be deleted, but no such employee was found.";
}
/** Return the error message for when an employee is to be added that already exists.
Analysis: Time = O(1) */
String addErrorMessage(Comparable employeeNumber)
{
return "\nAdd error encountered.\nAn employee with number "
+ employeeNumber + " was to be added, but an employee with that number was found.";
}
public static void main(String[] args) throws Exception
{
DemoMergeUpdate ts = new DemoMergeUpdate();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -