import java.io.*;
import java.util.*;
public class Driver {
BufferedReader b1, b2;
PrintWriter p1;
Scanner scan;
Driver person;
Person [] personAry, outAry;
Person dude;
public static void main(String[] args) {
new Driver(args[0], args[1], args[2]);
}
public Driver(String dbFile, String newFile, String sortBy) {
String line, last, first, snum;
int lineCount = 1;
try {
b2 = new BufferedReader(new FileReader(dbFile));
try {
b2.readLine();
while (b2.readLine()!= null){
lineCount++;
}
b2.close();
} catch (IOException e) {
System.out.println("Error reading from b2");
System.exit(1);
}
b1 = new BufferedReader(new FileReader(dbFile));
personAry = new Person [lineCount];
try {
b1.readLine();
for (int i = 0; i < lineCount; i++){
line = b1.readLine();
scan = new Scanner (line);
last = scan.next();
first = scan.next();
snum = scan.next();
dude = new Person(last,first,snum);
personAry[i] = dude;
if (sortBy == "1"){
dude.setSortMethod(1);
//sort by last name
}else if (sortBy == "2"){
dude.setSortMethod(2);
//sort by first name
}else if (sortBy == "3"){
dude.setSortMethod(3);
//sort by ssn
}
}
b1.close();
} catch (IOException e) {
System.out.println("Error reading from b1");
System.exit(1);
}
} catch (FileNotFoundException e) {
System.out.println("File Not Found");
System.exit(1);
}
//write the sorted personAry to a new file
try{
p1 = new PrintWriter (new FileWriter(newFile));
p1.flush();
p1.close();
} catch (IOException e) {
System.out.println("Error writing to p1");
System.exit(1);
}
}
} |