仍在学习技巧,我使用了混合的代码片段和自己的一些工作来完成此任务,但我一直坚持的是如何读回存储的文件。
在我的主类中,我使用了ObservableList,将其转换为ArrayList,然后使用FileOutputStream和ObjectOutputStream,
我希望能够通过FileInputStream和ObjectInputStream将数据“加载”回去。
我使用了This to help me save the data but can't work out how to read it back。
或者,如果您能想到一种替代方法来做我想做的事,那将会很不错。
我的主
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class JavaFXGUI extends Application {
Stage window;
TableView<Product> table;
TextField nameInput, priceInput, quantityInput;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
window = primaryStage;
window.setTitle("thenewboston - JavaFX");
//Name column
TableColumn<Product, String> nameColumn = new TableColumn<>("Name");
nameColumn.setMinWidth(200);
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
//Price column
TableColumn<Product, Double> priceColumn = new TableColumn<>("Price");
priceColumn.setMinWidth(100);
priceColumn.setCellValueFactory(new PropertyValueFactory<>("price"));
//Quantity column
TableColumn<Product, String> quantityColumn = new TableColumn<>("Quantity");
quantityColumn.setMinWidth(100);
quantityColumn.setCellValueFactory(new PropertyValueFactory<>("quantity"));
//Name input
nameInput = new TextField();
nameInput.setPromptText("Name");
nameInput.setMinWidth(100);
//Price input
priceInput = new TextField();
priceInput.setPromptText("Price");
//Quantity input
quantityInput = new TextField();
quantityInput.setPromptText("Quantity");
//Button
Button addButton = new Button("Add");
addButton.setOnAction(e -> addButtonClicked());
Button deleteButton = new Button("Delete");
deleteButton.setOnAction(e -> deleteButtonClicked());
HBox hBox = new HBox();
hBox.setPadding(new Insets(10, 10, 10, 10));
hBox.setSpacing(10);
hBox.getChildren().addAll(nameInput, priceInput, quantityInput, addButton, deleteButton);
table = new TableView<>();
table.setItems(getProduct());
table.getColumns().addAll(nameColumn, priceColumn, quantityColumn);
Scene scene = new Scene(new VBox(), 400, 350);
scene.setFill(Color.OLDLACE);
MenuBar menuBar = new MenuBar();
// --- Menu File
Menu menuFile = new Menu("File");
MenuItem save = new MenuItem("Save...");
menuFile.getItems().add(save);
save.setOnAction(e -> {
write(getProduct());
System.out.println("Saved File");
});
// --- Menu Edit
Menu menuEdit = new Menu("Edit");
// --- Menu View
Menu menuView = new Menu("View");
menuBar.getMenus().addAll(menuFile, menuEdit, menuView);
((VBox) scene.getRoot()).getChildren().addAll(menuBar, table, hBox);
window.setScene(scene);
window.show();
}
//Add button clicked
public void addButtonClicked() {
Product product = new Product();
product.setName(nameInput.getText());
product.setPrice(Double.parseDouble(priceInput.getText()));
product.setQuantity(Integer.parseInt(quantityInput.getText()));
table.getItems().add(product);
nameInput.clear();
priceInput.clear();
quantityInput.clear();
}
//Delete button clicked
public void deleteButtonClicked() {
ObservableList<Product> productSelected, allProducts;
allProducts = table.getItems();
productSelected = table.getSelectionModel().getSelectedItems();
productSelected.forEach(allProducts::remove);
}
//Get all of the products
public ObservableList<Product> getProduct() {
ObservableList<Product> products = FXCollections.observableArrayList();
products.add(new Product("Laptop", 859.00, 20));
products.add(new Product("Bouncy Ball", 2.49, 198));
products.add(new Product("Toilet", 99.00, 74));
products.add(new Product("The Notebook DVD", 19.99, 12));
products.add(new Product("Corn", 1.49, 856));
return products;
}
public void write(ObservableList<Product> productObservalble) {
try {
// write object to file
FileOutputStream fos = new FileOutputStream("Objectsavefile.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(new ArrayList<Product>(productObservalble));
oos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
产品.java
import java.io.Serializable;
public class Product implements Serializable {
private String name;
private double price;
private int quantity;
/**
* Create Constructor with empty data
*/
public Product() {
this.name = "";
this.price = 0;
this.quantity = 0;
}
/**
*
* @param name - Set the name for the Product
* @param price - Set the price of said Product
* @param quantity - Set how many of said product
*/
public Product(String name, double price, int quantity) {
this.name = name;
this.price = price;
this.quantity = quantity;
}
/**
* Get the name of a Product
*
* @return
*/
public String getName() {
return name;
}
/**
* Set the name of a Product
*
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* Get the price of a Product
*
* @return
*/
public double getPrice() {
return price;
}
/**
* Set the price of a Product
*
* @param price
*/
public void setPrice(double price) {
this.price = price;
}
/**
* Get the quantity of a Product
*
* @return
*/
public int getQuantity() {
return quantity;
}
/**
* Set the quantity of a Product
*
* @param quantity
*/
public void setQuantity(int quantity) {
this.quantity = quantity;
}
@Override
public String toString() {
return "Product [Name=" + name + ", Price=" + price + ", quantity="
+ quantity + "]";
}
}
最佳答案
只是做相反的事情:
public ObservableList<Product> read() throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(...));
List<Product> list = (List<Product>) ois.readObject();
ois.close();
return FXCollections.observableArrayList(list);
}
关于java - 将ArrayList读取到ObservableList中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43990798/