From de3953abaf39263dcc3b9f01584bfefdf81114fd Mon Sep 17 00:00:00 2001 From: jandroalbus Date: Sun, 16 Oct 2022 23:48:47 +0200 Subject: [PATCH] Commit inicial --- .../javafxopenwebinar/HelloApplication.java | 1 + .../javafxopenwebinar/StudentController.java | 54 +++++++++++++++---- 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/example/javafxopenwebinar/HelloApplication.java b/src/main/java/com/example/javafxopenwebinar/HelloApplication.java index 4529e88..47ce5c6 100644 --- a/src/main/java/com/example/javafxopenwebinar/HelloApplication.java +++ b/src/main/java/com/example/javafxopenwebinar/HelloApplication.java @@ -12,6 +12,7 @@ import javafx.stage.Stage; import java.io.IOException; + public class HelloApplication extends Application { @Override diff --git a/src/main/java/com/example/javafxopenwebinar/StudentController.java b/src/main/java/com/example/javafxopenwebinar/StudentController.java index 05798a3..6b3271a 100644 --- a/src/main/java/com/example/javafxopenwebinar/StudentController.java +++ b/src/main/java/com/example/javafxopenwebinar/StudentController.java @@ -7,7 +7,9 @@ import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.cell.PropertyValueFactory; +import java.io.*; import java.net.URL; +import java.nio.file.Files; import java.util.ResourceBundle; public class StudentController implements Initializable { @@ -23,16 +25,48 @@ public class StudentController implements Initializable { @Override public void initialize(URL url, ResourceBundle resourceBundle) { - studentId.setCellValueFactory(new PropertyValueFactory<>("studentId")); //<_El valor deberá ser el mismo!! - firstName.setCellValueFactory(new PropertyValueFactory<>("FirstName")); - lastName.setCellValueFactory(new PropertyValueFactory<>("LastName")); - tbData.setItems(FXCollections.observableArrayList( - new StudentModel(1, "student1", "lastname1"), - new StudentModel(2, "student2", "lastname2"), - new StudentModel(3, "student3", "lastname3"), - new StudentModel(4, "student4", "lastname4"), - new StudentModel(5, "student5", "lastname5") - )); + + /*Deserializar el archivo hecho en 8.2 por el fichero .dat del primer ejercicio */ + String fileToParse = "C:\\Users\\jandr\\Desktop\\Acceso a datos\\Container Ficheros\\FichPersona.dat"; + File file = new File(fileToParse); + + /*Creamos modelo objeto*/ + StudentModel student; + + /*Leemos archivo*/ + FileInputStream fIn; + try { + fIn = new FileInputStream(file); + } catch (FileNotFoundException e) { + throw new RuntimeException(e); + } + ObjectInputStream dataIS; + try { + dataIS = new ObjectInputStream(fIn); + } catch (IOException e) { + throw new RuntimeException(e); + } + + try { + + while (true) { + /*Cogemos los datos y rellenamos las celdas*/ + student = (StudentModel) dataIS.readObject(); + studentId.setCellValueFactory(new PropertyValueFactory<>("studentId")); + firstName.setCellValueFactory(new PropertyValueFactory<>("FirstName")); + lastName.setCellValueFactory(new PropertyValueFactory<>("LastName")); + tbData.setItems(FXCollections.observableArrayList( + new StudentModel(student.getStudentId(), student.getFirstName(), student.getLastName()) + )); + } + + } catch (ClassNotFoundException | IOException error) { + System.out.println(error.getMessage()); + } + + //Cerramos + try { dataIS.close(); } catch (IOException e) { throw new RuntimeException(e); } + } }