Question: The problem Java Exercise 17.2 Write a utility program that combines the files together into a new file using the following command: java Exercise17_12 SourceFile1
The problem Java
Exercise 17.2 Write a utility program that combines the files together into a new file using the following command:
java Exercise17_12 SourceFile1 ....SourceFilen TargetFile
The command combines SouceFile1... and SourceFilen into TargetFile.
Exercise 17.13 Rewrite Exercise 17.2 with a GUI.
I have worked on the program and it is signalling there is one error in this line.
public void joinFile(String filename, int numberOfPieces){
The code: import javafx.application.Application; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.control.TextField; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.geometry.Pos; import java.io.*; import javafx.scene.layout.GridPane; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox;
public class CombineFiles extends Application {
private TextField tfInputFile = new TextField(); private TextField tfNumberOfFiles = new TextField(); private Button btBrowse = new Button ("Browse"); private Button btStart = new Button ("Start"); @Override//Override the start method in the Application class public void start (Stage primaryStage) throws Exception { GridPane gridPane = new GridPane(); gridPane.add(new Label("Enter or chose a file:"),0,0); gridPane.add(tfInputFile,1,0); gridPane.add(new Label( "Specify the number of smaller files"),0,1); gridPane.add(tfNumberOfFiles,1,1); HBox hBox = new HBox(5); hBox.getChildren().add(btStart); hBox.setAlignment(Pos.CENTER); VBox vBox = new VBox(5); vBox.getChildren().addAll(new Label ("If the base file is named temp.txt with three pieces, " + " temp.txt.1, temp.txt.2, and temp.txt.3 are combined" + "into temp.txt"),gridPane,hBox); //create scene Scene scene = new Scene(vBox, 400,120); primaryStage.setScene(scene); primaryStage.setTitle("Gina's Combo"); primaryStage.show(); btStart.setOnAction(e-> { joinFile(tfInputFile.getText(), Integer.parseInt(tfNumberOfFiles.getText())); });
/** * * @param filename * @param numberOfPieces */ public void joinFile(String filename, int numberOfPieces){ try( // The last file TargetFile is for output BufferedOutputStream output = new BufferedOutputStream( new FileOutputStream(new File(filename))); ){ for ( int i =1; i< + numberOfPieces;i++){ try( BufferedInputStream input = new BufferedInputStream( new FileInputStream(new File(filename))); ){ int value; while ((value = input.read())!= -1) { output.write(value); } } } } catch (IOException ex) { } }
public static void main(String[] args) { Application.launch(args); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
