Advertisement
Utshaw

readFromTextFile

Nov 30th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. package testerpackage;
  2.  
  3.  
  4.  
  5. import java.io.File;
  6. import java.io.IOException;
  7. import java.nio.file.Files;
  8. import java.util.logging.Level;
  9. import java.util.logging.Logger;
  10. import javafx.application.Application;
  11. import javafx.event.ActionEvent;
  12. import javafx.event.EventHandler;
  13. import javafx.scene.Scene;
  14. import javafx.scene.control.Button;
  15. import javafx.scene.layout.VBox;
  16. import javafx.stage.FileChooser;
  17. import javafx.stage.Stage;
  18.  
  19. /**
  20. * Created by Utshaw on 11/30/2016.
  21. */
  22. public class Driver6 extends Application {
  23.  
  24. @Override
  25. public void start(Stage primaryStage) {
  26. Button btnLoad = new Button("Load");
  27. btnLoad.setOnAction(btnLoadEventListener);
  28.  
  29. VBox rootBox = new VBox();
  30. rootBox.getChildren().add(btnLoad);
  31.  
  32. Scene scene = new Scene(rootBox, 300, 300);
  33.  
  34. primaryStage.setTitle("java-buddy.blogspot.com");
  35. primaryStage.setScene(scene);
  36. primaryStage.show();
  37. }
  38.  
  39. EventHandler<ActionEvent> btnLoadEventListener
  40. = (ActionEvent event) -> {
  41. FileChooser fileChooser = new FileChooser();
  42. fileChooser.getExtensionFilters()
  43. .addAll(
  44. new FileChooser.ExtensionFilter("TXT files (*.TXT)", "*.TXT"),
  45. new FileChooser.ExtensionFilter("txt files (*.txt)", "*.txt"));
  46.  
  47. File file = fileChooser.showOpenDialog(null);
  48. if (file != null) {
  49. try {
  50. Files.lines(file.toPath()).forEach(System.out::println);
  51. } catch (IOException ex) {
  52. Logger.getLogger(Driver6.class.getName())
  53. .log(Level.SEVERE, null, ex);
  54. }
  55. }
  56. };
  57.  
  58. public static void main(String[] args) {
  59. launch(args);
  60. }
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement