如何将.dat文件转换为ObservableList?

问题描述

我正在尝试将 ArrayList 转换为 ObservableList ,我以为我做对了,但是除了主窗格之外,什么都没有弹出。我希望读取一个名称 .dat文件,该名称会出现在列表中,但是当我运行该程序时,除了标题和窗口本身之外,什么都没有显示 .dat文件显示名称,我会缺少什么吗?

public class PeopleBinary extends Application {
    private ArrayList<Person> people = new ArrayList<>();
    private ObservableList<Person> obserPerson;
    
    public void read() throws Exception {
        ObjectInputStream input = new ObjectInputStream(new FileInputStream("people.dat"));
        try {
            while (input.available() > 0) {
                Person temp = (Person) input.readobject();
                people.add(temp);
            }
            input.close();
        } 
        catch (IOException e) {
            System.out.println("Problem reading file - File will be terminated.");
            System.exit(0);
        }
    }
    
    public void write() {
        try {
            ObjectOutputStream peopleBin = new ObjectOutputStream(new FileOutputStream("people.dat"));
            for (Person p: obserPerson) {
                peopleBin.writeUTF(p.getFirstName());
                peopleBin.writeUTF(p.getLastName());
                peopleBin.writeUTF(p.getCity());
                peopleBin.writeInt(p.getIdNum());
            }
            peopleBin.close();
        } 
        catch (IOException e) {
            System.out.println("Unable to write to file - End of program.");
            System.exit(0);
            e.printstacktrace();
        }
    }

    @Override
    public void start(Stage primaryStage) {
        obserPerson = FXCollections.observableArrayList(people);
        ListView<Person> perList = new ListView<Person>();
        perList.setItems(obserPerson);
        perList.setPrefSize(400,400);
        perList.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
            
        borderpane mainPane = new borderpane(); 
        mainPane.setLeft(new ScrollPane(perList));
            
        Scene scene = new Scene(mainPane,400,400);
        primaryStage.setTitle("Person Library");
        primaryStage.setScene(scene);
        primaryStage.show();
    }   

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)