17.13 - Combine files GUI I successfully wrote code for the scenario below. I wo
ID: 3667574 • Letter: 1
Question
17.13 - Combine files GUI
I successfully wrote code for the scenario below. I would like someone from your team to rewrite the code using a different approach. This will allow me to see how someone else may go about coding this program and ultimately coming out with the same results. Please code using Java and verify it will run in a Java IDE. My code can be found below. Thanks.
*17.12 (Combine files) Write a utility program that combines the files together into anew file using the following command:
java Exercise17_12 SourceFile1 . . . SourceFilen TargetFile
The command combines SourceFile1, . . . , and SourceFilen into TargetFile.
17.13 (Combine files GUI) Rewrite Exercise 17.12 with a GUI, as shown in
Figure below:
This is my code for the program. It runs successfully, no issues. Please re-code using a different approach so I can compare and contrast a different ways to write the same program.
package Chapter_17;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
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.scene.layout.GridPane;
import javafx.stage.Stage;
import java.io.*;
/**
* (Combine files GUI)
* Rewrite Exercise 17.12 with a GUI, as shown in Figure 17.21b.
* Created by Luiz Arantes Sa on 11/30/14.
*/
public class Exercise_13 extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
FileCombinerPane pane = new FileCombinerPane();
primaryStage.setScene(new Scene(pane));
primaryStage.setTitle("File Combiner");
primaryStage.show();
}
private class FileCombinerPane extends BorderPane {
TextField tfFilename;
TextField tfNumOfDivision;
Button btnSplit;
FileCombinerPane() {
tfFilename = new TextField();
tfNumOfDivision = new TextField();
btnSplit = new Button("Combine Files");
btnSplit.setOnAction(e -> combineFiles());
Label lblDescription = new Label(
"If the base file is named temp.txt with 3 peices, " +
"temp.txt.1, temp.txt.2, temp.txt.3 are combined into temp.txt " +
"Enter absolute path if file is not in working directory...");
Label lblFilename = new Label("Base Filename:");
Label lblNumOfDivision = new Label("Number Of Files:");
// Format gridPane and nodes
GridPane gridPane = new GridPane();
gridPane.add(lblDescription, 0, 0,2,1);
gridPane.addRow(1, lblFilename, tfFilename);
gridPane.addRow(2, lblNumOfDivision, tfNumOfDivision);
gridPane.setHgap(10);
gridPane.setVgap(10);
gridPane.setPadding(new Insets(20));
GridPane.setFillWidth(tfNumOfDivision, false);
tfNumOfDivision.setPrefColumnCount(4);
setCenter(gridPane);
setBottom(btnSplit);
setAlignment(btnSplit, Pos.CENTER);
setPadding(new Insets(10));
}
public void combineFiles() {
File[] splitFiles = new File[Integer.parseInt(tfNumOfDivision.getText())];
File targetFile = new File(tfFilename.getText());
// Verify if src files exists
for (int i = 0; i < splitFiles.length; i++) {
splitFiles[i] = new File(targetFile.getAbsolutePath() + "." + (i + 1) );
if (!splitFiles[i].isFile()) {
System.out.println(splitFiles[i].getAbsolutePath() + " file not found.");
}
}
// Combine files into targetFile
try (BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(targetFile))) {
int buffer;
for (File f : splitFiles) {
try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(f))) {
while ((buffer = in.read()) != -1)
out.write(buffer);
}
}
} catch (IOException ex) {
System.out.println("Error combining files");
}
}
}
public static void main(String[] args) {
Application.launch(args);
}
}
Explanation / Answer
No need of coding again..This is the best approach and your code is very simple.
If you want I wil do it using Applets(but as of my knowledge Javafx far better than Applets.)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.