javafx file reading code example
Example: write data to a file GUI javafx
package com.fileio;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class FileIO extends Application {
TextField textField;
Label text, clicked;
Button button, clickButton;
BorderPane BPane;
private boolean _clickMeMode = true;
@Override
public void start (Stage primaryStage) {
BPane = new BorderPane();
BPane.setId("grid-pane BPane.getStyleClass().add("pane-styles");
Scene Scene = new Scene(BPane, 300, 200);
Scene.getStylesheets().add(this.getClass().getResource(
"EssentialsJPL.css").toExternalForm
primaryStage.setTitle("FileIO Application");
primaryStage.setScene(Scene);
text = new Label("Text to save to file:");
textField = new TextField();
textField.setMaxWidth(200);
button = new Button("Click Me");
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
Object source = event.getSource();
String s = null;
if (_clickMeMode) {
FileInputStream in = null;
FileOutputStream out = null;
try {
String text = textField.getText();
byte b[] = text.getBytes();
String outputFileName = System.getProperty("user.home",
File.separatorChar + "home"
+ File.separatorChar + "monica")
+ File.separatorChar + "text.txt";
out = new FileOutputStream(outputFileName);
out.write(b);
out.close();
textField.setText("");
String inputFileName = System.getProperty("user.home",
File.separatorChar + "home"
+ File.separatorChar + "monica")
+ File.separatorChar + "text.txt";
File inputFile = new File(inputFileName);
in = new FileInputStream(inputFile);
byte bt[] = new byte[(int) inputFile.length()];
in.read(bt);
s = new String(bt);
in.close();
} catch (java.io.IOException e) {
System.out.println("Cannotss text.txt");
} finally {
try {
in.close();
out.close();
} catch (java.io.IOException e) {
System.out.println("Cannote");
}
}
textField.setText("");
text.setText("Text retrieved from file: \n\n" + s);
BPane.getChildren().remove(textField);
button.setText("Click Again");
_clickMeMode = false;
} else {
text.setText("Text to save to file:");
BPane.getChildren().add(textField);
textField.setText("");
button.setText("Click Me");
_clickMeMode = true;
}
}
});
BPane.setTop(text);
BPane.setCenter(textField);
BPane.setBottom(button);
primaryStage.show();
}
public static void main(String[] args){
launch(args);
}
}