2017-05-16 34 views
1

仍然在學習繩索,我使用了代碼片段的混合搭配以及我自己的一些工作來實現這一點,但是我堅持的是如何讀取文件存儲。將ArrayList讀入ObservableList

在我的主類,我已經使用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(); 
    } 

} 

} 

Products.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 + "]"; 

} 

} 

回答

1

只是做相反:

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); 

} 
+0

所以我怎樣才能把它變成的閱讀方法? – Tumnus

+0

歡呼我會盡力得到這個工作。 – Tumnus