Posterior de una prolongada búsqueda de información pudimos solucionar esta cuestión que suelen tener algunos los lectores. Te dejamos la respuesta y nuestro objetivo es servirte de mucha ayuda.
Ejemplo: escribir datos en un archivo GUI javafx
packagecom.fileio;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjavafx.application.Application;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.Scene;importjavafx.scene.control.Button;importjavafx.scene.control.Label;importjavafx.scene.control.TextField;importjavafx.scene.layout.BorderPane;importjavafx.stage.Stage;/**
*
* @author mpawlan
*/publicclassFileIOextendsApplicationTextField textField;Label text, clicked;Button button, clickButton;BorderPaneBPane;privateboolean _clickMeMode =true;@Overridepublicvoid start (Stage primaryStage)//Create GridPaneBPane=newBorderPane();BPane.setId("grid-pane BPane.getStyleClass().add("pane-styles");//Create Scene and add GridSceneScene=newScene(BPane,300,200);Scene.getStylesheets().add(this.getClass().getResource("EssentialsJPL.css").toExternalForm
//Create the stage and add the scene
primaryStage.setTitle("FileIO Application");
primaryStage.setScene(Scene);
text =newLabel("Text to save to file:");
textField =newTextField();
textField.setMaxWidth(200);
button =newButton("Click Me");
button.setOnAction(newEventHandler<ActionEvent>()@Overridepublicvoidhandle(ActionEvent event)Object source = event.getSource();String s =null;//Variable to display text read from fileif(_clickMeMode)FileInputStream in =null;FileOutputStream out =null;try//Code to write to fileString text = textField.getText();byte b[]= text.getBytes();String outputFileName =System.getProperty("user.home",File.separatorChar +"home"+File.separatorChar +"monica")+File.separatorChar +"text.txt";
out =newFileOutputStream(outputFileName);
out.write(b);
out.close();//Clear text field
textField.setText("");//Code to read from fileString inputFileName =System.getProperty("user.home",File.separatorChar +"home"+File.separatorChar +"monica")+File.separatorChar +"text.txt";File inputFile =newFile(inputFileName);
in =newFileInputStream(inputFile);byte bt[]=newbyte[(int) inputFile.length()];
in.read(bt);
s =newString(bt);
in.close();catch(java.io.IOException e)System.out.println("Cannotss text.txt");finallytry
in.close();
out.close();catch(java.io.IOException e)System.out.println("Cannote");//Clear text field
textField.setText("");//Display text read from file
text.setText("Text retrieved from file: nn"+ s);BPane.getChildren().remove(textField);
button.setText("Click Again");
_clickMeMode =false;else//Save text to file
text.setText("Text to save to file:");BPane.getChildren().add(textField);
textField.setText("");
button.setText("Click Me");
_clickMeMode =true;);//Set positions for each control in the BorderPaneBPane.setTop(text);BPane.setCenter(textField);BPane.setBottom(button);//Show the scene
primaryStage.show();//main methodpublicstaticvoidmain(String[] args)launch(args);
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)