package addBk.addressBook;
import javax.swing.*;
import java.awt.*;
import java.io.*;
public class CSVMerge {
static boolean isSwing = false;
public CSVMerge(AddressBookDatabase abd) {
try {
BufferedReader br = getReader("select a CSV file");
for (String line = br.readLine(); line != null;
line = br.readLine())
abd.addRecord(
AddressBookRecord.getRecord(line));
br.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
public static File getFile(String prompt) {
if (isSwing)
return Futil.JGetReadFile(prompt);
return new File(Futil.getReadFileName(prompt));
}
public static BufferedReader getReader(String prompt)
throws FileNotFoundException {
return new BufferedReader(
new InputStreamReader(
new FileInputStream(getFile(prompt))));
}
static class Futil {
public static File JGetReadFile(
String prompt) {
JFileChooser jfc = new JFileChooser();
jfc.showOpenDialog(new JFrame());
return jfc.getSelectedFile();
}
public static String getReadFileName(
String prompt) {
FileDialog fd = new FileDialog(
new Frame(), prompt);
fd.setVisible(true);
fd.setVisible(false);
return fd.getDirectory() + fd.getFile();
}
public static String getWriteFileName(
String prompt) {
FileDialog fd = new FileDialog(
new Frame(), prompt, FileDialog.SAVE);
fd.setVisible(true);
fd.setVisible(false);
return fd.getDirectory() + fd.getFile();
}
} }