17.13 - Combine files GUI I successfully wrote code for the scenario below. I wo
ID: 3667642 • 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
/**
* Program: Ch19Prog1.java
* Programmer: Andrew Buskov
* Class: CIS 249
* Date: Jan 19, 2013
* Purpose: To combine a number of files into one single file.
* Example: file.ext.1 + file.ext.2 + file.ext.3 = file.ext
*/
//package Ch19.Prog1;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.*;
public class HelloWorld extends JFrame {
private static final long serialVersionUID = 1L; // eclipse complains
private JTextField jtfBaseFile = new JTextField(20);
private JTextField jtfNumOfFiles = new JTextField(3);
private JButton jbtStart = new JButton("Start");
public HelloWorld(){
// construct base file panel
JPanel panelBase = new JPanel(new BorderLayout());
panelBase.add(new JLabel("Enter the base file: "), BorderLayout.WEST);
panelBase.add(jtfBaseFile, BorderLayout.EAST);
// construct number of files panel
JPanel panelNumFiles = new JPanel(new BorderLayout());
panelNumFiles.add(new JLabel("Specify the number of smaller files: "), BorderLayout.WEST);
panelNumFiles.add(jtfNumOfFiles);
//construct the main panel to hold everything
JPanel panel = new JPanel(new GridLayout(4,1));
JTextArea jlb = new JTextArea("Ifthe base file is named temp.txt with three pieces, temp.txt.1, temp.txt.2, and temp.txt.3 are combined into temp.txt");
jlb.setWrapStyleWord(true);
jlb.setLineWrap(true);
// add everything to the main panel
panel.add(jlb);
panel.add(panelBase);
panel.add(panelNumFiles);
panel.add(jbtStart);
// add panel to the frame
this.add(panel);
// create the actionlistener for the start button
jbtStart.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
// checking to make sure that the fields aren't empty
if (!jtfBaseFile.getText().equals("") && !jtfNumOfFiles.getText().equals("")){
combineFiles(jtfBaseFile.getText(), Integer.parseInt(jtfNumOfFiles.getText()));
} else {
JOptionPane.showMessageDialog(null,"You must enter values into both boxes!");
}
}
});
}
public void combineFiles(String fileName, int numOfFiles){
try {
// first, create archive dir if not available.
File dir = new File("./archives/");
dir.mkdir();
BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(new File(dir + "/" + fileName)));
for(int i=1; i<=numOfFiles;i++){
BufferedInputStream input = new BufferedInputStream(new FileInputStream(new File(dir + "/" + fileName + "." + i)));
int value;
// write output as long as there is a file being read from (value)
while ((value = input.read()) != -1){
output.write(value);
}
input.close();
}
output.close();
} catch (IOException ex){
ex.printStackTrace();
}
}
public static void main(String[] args){
HelloWorld frame = new HelloWorld();
frame.setSize(500, 200);
frame.setTitle("Combine Files");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.