package filehandler; import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.layout.VBox; import javafx.scene.layout.HBox; import...

can you please help me?


package filehandler; import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.layout.VBox; import javafx.scene.layout.HBox; import javafx.scene.control.Button; import javafx.scene.control.TextField; import javafx.scene.control.TextArea; import javafx.scene.control.Label; import javafx.geometry.Pos; import javafx.geometry.Insets; public class FileHandlerGUI extends Application { private String[] messageAndContent = null; private String message = ""; public static void main(String[] args) { launch(args); } public void start(Stage primaryStage) { // Create the controls for the application Button createFile = new Button("Create a File"); Button writeFile = new Button("Write to File"); Button readFile = new Button("Read from File"); Button deleteFile = new Button("Delete a File"); TextField fileName = new TextField("Enter the name of the file here..."); TextArea fileContent = new TextArea("Enter content to write to the file here..."); Label feedbackLabel = new Label(""); // Create the VBox for the four buttons VBox buttons = new VBox(10, createFile, writeFile, readFile, deleteFile); // Create the VBox for the text inputs VBox textControls = new VBox(15, fileName, fileContent, feedbackLabel); // Create the HBox for the root of the scene HBox root = new HBox(50, buttons, textControls); root.setPadding(new Insets(100, 0, 0, 0)); root.setAlignment(Pos.CENTER); // Set the style classes for the buttons and the functionality for each button createFile.getStyleClass().add("create-button"); writeFile.getStyleClass().add("write-button"); readFile.getStyleClass().add("read-button"); deleteFile.getStyleClass().add("delete-button"); // Call the createFile method and get the message for the feedback label createFile.setOnAction(event -> { message = FileHandler.makeFile(fileName.getText()); feedbackLabel.setText(message); }); // Call the writeFile method and get the message for the feedback label writeFile.setOnAction(event -> { message = FileHandler.writeFile(fileName.getText(), fileContent.getText()); feedbackLabel.setText(message); }); // Call the readFile method // Get the message for the feedback label and the content for the text area readFile.setOnAction(event -> { messageAndContent = FileHandler.readFile(fileName.getText()); fileContent.setText(messageAndContent[0]); feedbackLabel.setText(messageAndContent[1]); }); // Call the deleteFile method and get the message for the feedback label deleteFile.setOnAction(event -> { message = FileHandler.deleteFile(fileName.getText()); feedbackLabel.setText(message); }); // Create the scene and set up the style Scene scene = new Scene(root, 1200, 600); scene.getStylesheets().add(this.getClass().getResource("style.css").toExternalForm()); // Set up the stage primaryStage.setScene(scene); primaryStage.setTitle("Notepad"); primaryStage.show(); } } Programming Fundamentals II Sec. 601 Lab Assignment #10 File Handler Due date: 11/18/20 at 11:59 pm Purpose: The lab this week focuses on Exception handling as it relates to file management. You should be familiar with the concepts of Exception handling and throwing Exceptions. Task: Create a project called FileHandler_FirstName_LastName or Lab10_FirstName_LastName. The program will consist of three files: the FileHandler.java, FileHandlerGUI.java, and style.css. The FileHandlerGUI.java and style.css files are provided on Blackboard. Remember to include comments summarizing the program in the FileHandler.java file. 1. Review the code provided on Blackboard to understand which controls the FileHandler methods are used with. Depending on the method, inputs come from the TextField and TextArea and outputs go to the TextArea and Label. You should also be aware of the names for the four methods that will be implemented in the FileHandler class. 2. The makeFile method takes a String with the name of the file to create and returns a message about whether or not the file was successfully created. Construct an object of the File class using the passed in name. Within a try block, use the createNewFile method of the File class to create the file. The createNewFile method returns true if this operation is successful. If the operation is successful, return the String “ successfully created.” If the operation is not successful, return the String “Something went wrong while creating the file.” If an exception is thrown, return the default exception message. 3. The readFile method takes the name of a file in order to construct a Scanner object to read from that file. This method also returns a String array that consists of two Strings: a String for the content of the file to be displayed in the TextArea (position 0) and a String for the error message to be displayed in the Label (position 1). Construct the String array and initialize both values to empty Strings. Use a try-with-resources block to read from the file and catch a FileNotFoundException if the file does not exist. If the file does not exist, the content should be an empty String and the error message should be the String: “File not found.” If the file does exist, the content should be a String containing the file’s contents and the error message should be the String: “Successfully read from.” To get the file’s contents, use a while loop and String concatenation to add each line of the file to the String at position 0 of the String array. 4. The writeFile method takes the name of the file and the contents to write to the file in order to construct a PrintWriter to write to that file. This method also returns a String for the error message to be displayed in the Label. Use a try-catch block to create a File object. Rather than writing to the file if it does not exist (the default behavior of PrintWriter), check if the file exists first and throw a FileNotFoundException if it does not exist. If this Exception is thrown, return the String: “File not found.” Within the try block, use a try-with-resources block to write to the file. If an Exception is thrown, return the String: “Could not write to the file.” If no Exceptions are thrown, return the String: “Successfully wrote to .” 5. The deleteFile method takes the name of the file to construct a File object. This method also returns a String for the error message to be displayed in the Label. Similar to the createFile method, this method checks if the file is successfully deleted. Use the delete method of the File class and return the appropriate String based on what the delete method returns. If the delete method returns true, return the String: “ successfully deleted.” If the delete method returns false, return the String: “ was not deleted. Please check if the file exists.” Criteria: The comment summarizing the program is worth 5 points. The createFile method is worth 10 points. The readFile method is worth 45 points. The writeFile method is worth 30 points. The deleteFile method is worth 10 points.
Nov 05, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here