我有一个项目,其中必须显示保存在CSV文件中的所有先前操作。 CSV文件中的数据将转换为ArrayList并使用类对象返回给用户。
public static ArrayList<String> csv_read() throws IOException {
ArrayList<String> fileText = new ArrayList<>();
String row;
try (BufferedReader bufferedReader = new BufferedReader(new FileReader("C:\\Users\\hatim\\IdeaProjects\\mini\\src\\sample\\calculations.csv")))
{
while ((row = bufferedReader.readLine()) != null) {
fileText.add(row);
}
}
return fileText;
}
上面是将CSV文件转换为ArrayList并返回给类的地方。
VBox secondaryLayout = new VBox();
Scene secondScene = new Scene(secondaryLayout, 400, 350);
// New window (Stage)
Stage newWindow = new Stage();
newWindow.setTitle("Previous calculations");
Socket socket = new Socket("localhost", 5001);
ObjectOutputStream output_client = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream input_client = new ObjectInputStream(socket.getInputStream());
output_client.writeObject(new subCOM("read"));
subCOM com = (subCOM) input_client.readObject();
ArrayList<String> result = com.getStringArray();
ObservableList<String> olist = FXCollections.observableArrayList(result);//data
TableView<String> table = new TableView<>();
table.setItems(olist);
table.getColumns().setAll();
table.setPrefWidth(450);
table.setPrefHeight(300);
secondaryLayout.getChildren().add(table);
// Set position of second window, related to primary window.
newWindow.setX(stage.getX() + 200);
newWindow.setY(stage.getY() + 100);
newWindow.setScene(secondScene);
newWindow.show();
上面是接收ArrayList(com.getStringArray())并制成可观察列表的地方
CSV文件主要有4列(“旅行距离”,“燃油效率”,“燃料成本”,“总成本”) 我只想在表格视图中显示所有数据。