Вот некоторый код, который мы написали в 2015 году для тестирования с использованием файла CSV в качестве базы данных
Он поддерживается классом модели, поэтому мы используем контроллер представления модели
первая запись кода - это то, как мы сохраняем данныето, как мы выбираем данные
@FXML
private void onSave(ActionEvent e) throws IOException {
// TableData = FXCollections.observableArrayList();
// Hold data for the table
if (txfFName.getText().isEmpty() || txfLName.getText().isEmpty()|| txfPNum.getText().isEmpty() || txfEMail.getText().isEmpty()) {
Alert alert = new Alert(AlertType.WARNING);
alert.initStyle(StageStyle.UTILITY);
alert.setTitle("Information");
alert.setHeaderText("");
alert.setContentText("Data Entry NOT Complete\n"
+ "\nENTER New Data in All Fields and Alt + S to SAVE");
alert.showAndWait();
if (txfFName.getText().isEmpty()) {
txfFName.requestFocus();
return;
}
if (txfLName.getText().isEmpty()) {
txfLName.requestFocus();
return;
}
if (txfPNum.getText().isEmpty()) {
txfPNum.requestFocus();
return;
}
if (txfEMail.getText().isEmpty()) {
txfEMail.requestFocus();
return;
}
return;
}
if (txfFName.getText().length() > 15) {
Alert alert = new Alert(AlertType.WARNING);
alert.initStyle(StageStyle.UTILITY);
alert.setTitle("Information");
alert.setHeaderText("");
alert.setContentText("First Name Max Length is 15 Characters");
alert.showAndWait();
txfFName.requestFocus();
return;
}
if (txfLName.getText().length() > 16) {
Alert alert = new Alert(AlertType.WARNING);
alert.initStyle(StageStyle.UTILITY);
alert.setTitle("Information");
alert.setHeaderText("");
alert.setContentText("Last Name Max Length is 16 Characters");
alert.showAndWait();
txfLName.requestFocus();
return;
}
if (txfPNum.getText().length() > 12) {
Alert alert = new Alert(AlertType.WARNING);
alert.initStyle(StageStyle.UTILITY);
alert.setTitle("Information");
alert.setHeaderText("");
alert.setContentText("Phone Number Max Length is 12 Characters");
alert.showAndWait();
txfPNum.requestFocus();
return;
}
if (txfEMail.getText().length() > 30) {
Alert alert = new Alert(AlertType.WARNING);
alert.initStyle(StageStyle.UTILITY);
alert.setTitle("Information");
alert.setHeaderText("");
alert.setContentText("E-Mail Max Length is 30 Characters");
alert.showAndWait();
txfEMail.requestFocus();
return;
}
String fname = txfFName.getText().trim();
String lname = txfLName.getText().trim();
String pnum = txfPNum.getText().trim();
String email = txfEMail.getText().trim();
String data = (fname + "," + lname + "," + pnum + "," + email + "," + '\r');
File dirPath = new File("C:/A_CSV");
dirPath.mkdirs();// Make the directory
File file = new File(dirPath, "People.csv");
if (!file.exists()) {
file.createNewFile();// Create an empty text file
}
Это код данных выборки
@FXML
// Load CSV Data
private void onLoad(ActionEvent e) throws IOException {
// TableData = FXCollections.observableArrayList();//Hold data for the table
File fileInfo = new File("C:/A_CSV/People.csv");
if (fileInfo.length() == 0) {
Alert alert = new Alert(AlertType.WARNING);
alert.initStyle(StageStyle.UTILITY);
alert.setTitle("Information");
alert.setHeaderText("");
alert.setContentText("No Data in File at " + fileInfo + "\n"
+ "\nEnter Data and Save");
alert.showAndWait();
return;
}
fc.setTitle("Load Contacts Info");
fc.setInitialDirectory(new File("C:/"));
fc.setInitialDirectory(new File("C:/A_CSV"));
fc.setInitialFileName("People.csv");
File file = fc.showOpenDialog(null);
if (file == null) {
return;
}
String correctFile = file.getName();
if (!(correctFile.matches("People.csv"))) {
Alert alert = new Alert(AlertType.ERROR);
alert.initStyle(StageStyle.UTILITY);
alert.setTitle("Information");
alert.setHeaderText("");
alert.setContentText("The File at " + file + "\n\n"
+ "is NOT accociated with this application\n\n"
+ "Select the File at " + fileInfo);
alert.showAndWait();
return;
}
unAdd();
table.getItems().clear();// Clears the table
onShowTableData();
}
Вот класс модели Просто группа получателей и установщиков
public class Person {
// This belongs to CSV Controller
// ===============================
private final StringProperty firstName = new SimpleStringProperty(this,"firstName", null);
private final StringProperty lastName = new SimpleStringProperty(this,"lastName", null);
private final StringProperty phoneNumber = new SimpleStringProperty(this,"phoneNumber", null);
private final StringProperty emailAddress = new SimpleStringProperty(this,"emailAddress", null);
public Person() {
this(null, null, null, null);
}
public Person(String firstName, String lastName, String phoneNumber,String emailAddress) {
this.firstName.set(firstName);
this.lastName.set(lastName);
this.phoneNumber.set(phoneNumber);
this.emailAddress.set(emailAddress);
}
/* firstName Property */
public final String getFirstName() {// 1
return firstName.get();
}
public final void setFirstName(String firstName) {// 1
firstNameProperty().set(firstName);
}
public final StringProperty firstNameProperty() {// 1
return firstName;
}
public final String getLastName() {// 2
return lastName.get();
}
public final void setLastName(String lastName) {// 2
lastNameProperty().set(lastName);
}
public final StringProperty lastNameProperty() {// 2
return lastName;
}
public final String getPhoneNumber() {// 3
return phoneNumber.get();
}
public final void setPhoneNumber(String phoneNumber) {// 3
phoneNumberProperty().set(phoneNumber);
}
public final StringProperty phoneNumberProperty() {// 3
return phoneNumber;
}
public final String getEmailAddress() {
return emailAddress.get();
}
public final void setEmailAddress(String emailAddress) {
emailAddressProperty().set(emailAddress);
}
public final StringProperty emailAddressProperty() {
return emailAddress;
}
Если вы хотите весь проект, дайте мне знать, и я заархивирую его и положу на GitHub