Question
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
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.
My code: public class CombineFiles extends Application { @Override//Override the start method in the Application class
private TextField tfInputFile = new TextField(); private TextField tfNumberOfFiles = new TextField(); private Button btBrowse = new Button ("Browse"); private Button btStart = new Button ("Start"); 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.getChildern().add(btStart); HBox.setAlignment(Pos.Center); VBox vBox = new VBox(5); vBox.getChildern().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(new Scene(pane)); primaryStage.setTitle("Gina's Combo"); primaryStage.show(); } btStart.setOnAction(e-> { joinFile(tfInputFile.getText(), integer.parseInt(tfNumberOfFiles.getText())); });
) public void joinFile (String gilename, int numberOfPieces){ try( // The last file TargetFile is for output BufferedOutputStream = new BufferedOutputStream( new FileOutputStream( new File (filename))); ){ for ( int i =1; i< + numberOfPieces;i++){ try( BufferedInputStream input = new BufferedInputStream( new FileInputStream(new File (filename \"."\ i ))); int value; while ((value + input.read()) !-1) { output.write(value); } } } } catch (IO Exception ex) { ex.printStackTrace();
} }
public static void main(String[] args) { Application.launch(args); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started