Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need to make this a GUI.... import java.io.*; import java.awt.*; import java.a

ID: 3639605 • Letter: I

Question

I need to make this a GUI....

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class Ch19Lab3 extends JFrame {
private JTextField jtfInputFile = new JTextField(20);
private JTextField jtfNumberOfFiles = new JTextField(2);
private JButton jbtBrowse = new JButton("Browse");
private JButton jbtStart = new JButton("Start");

public Ch19Lab3 () {
JPanel panel1 = new JPanel(new BorderLayout());
panel1.add(new JLabel("Enter or choose a file: "), BorderLayout.WEST);
panel1.add(jtfInputFile, BorderLayout.CENTER);
panel1.add(jbtBrowse, BorderLayout.EAST);

JPanel panel2 = new JPanel(new BorderLayout());
panel2.add(new JLabel("Specify the number of smaller files: "),
BorderLayout.WEST);
panel2.add(jtfNumberOfFiles, BorderLayout.CENTER);

JPanel panel = new JPanel(new GridLayout(5, 1));
JTextArea jta = new JTextArea(
"If you split a file named temp.txt into 3 smaller files, the three smaller files are temp.txt.1, temp.txt.2, and temp.txt.3.");
jta.setWrapStyleWord(true);
jta.setLineWrap(true);

panel.add(jta);
panel.add(panel1);
panel.add(panel2);
panel.add(jbtStart);
add(panel);


jbtStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
splitFile(jtfInputFile.getText(), Integer.parseInt(jtfNumberOfFiles.getText()));
}
});

jbtBrowse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showOpenDialog(null)
== JFileChooser.APPROVE_OPTION) {
// Get the selected file
// Get the selected file
java.io.File file = fileChooser.getSelectedFile();
jtfInputFile.setText(file.toString());
}
}
});
}

public void splitFile(String filename, int numberOfPieces) {
try {
BufferedInputStream input = new BufferedInputStream(
new FileInputStream(new File(filename)));

for (int i = 1; i <= numberOfPieces; i++) {
BufferedOutputStream output = new BufferedOutputStream(
new FileOutputStream(new File(filename + "." + i)));
int value;
int count = 0;
// What is wrong if these two conditions are placed in a different
// order?
while (count++ < splitFileSize && (value = input.read()) != -1) {
output.write(value);
}
output.close();
}

input.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}

public static void main(String[] args) {
Ch19Lab3 frame = new Ch19Lab3();
frame.pack();
frame.setTitle("Exercise29_15: Split a File");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null); // Center the frame
frame.setVisible(true);
}
}

**Instead of a BufferedOutputStream this needs to use BufferedInputStream, the variable for counting needs to be taken out and the while loop will have parameters of:
value = input.read()) != -1
Where the variable value is the name of the integer created for a value, input is the name of the BufferedInputStream.

Explanation / Answer

Try the below given code, It works fine and we get the number of files as given 'Specify smaller files' created in the given directory. Please find below the code and run it. import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class Ch19Lab3 extends JFrame { private JTextField jtfInputFile = new JTextField(20); private JTextField jtfNumberOfFiles = new JTextField(2); private JButton jbtBrowse = new JButton("Browse"); private JButton jbtStart = new JButton("Start"); public Ch19Lab3() { JPanel panel1 = new JPanel(new BorderLayout()); panel1.add(new JLabel("Enter or choose a file: "), BorderLayout.WEST); panel1.add(jtfInputFile, BorderLayout.CENTER); panel1.add(jbtBrowse, BorderLayout.EAST); JPanel panel2 = new JPanel(new BorderLayout()); panel2.add(new JLabel("Specify the number of smaller files: "), BorderLayout.WEST); panel2.add(jtfNumberOfFiles, BorderLayout.CENTER); JPanel panel = new JPanel(new GridLayout(5, 1)); JTextArea jta = new JTextArea( "If you split a file named temp.txt into 3 smaller files, the three smaller files are temp.txt.1, temp.txt.2, and temp.txt.3."); jta.setWrapStyleWord(true); jta.setLineWrap(true); panel.add(jta); panel.add(panel1); panel.add(panel2); panel.add(jbtStart); add(panel); jbtStart.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { splitFile(jtfInputFile.getText(), Integer.parseInt(jtfNumberOfFiles.getText())); } }); jbtBrowse.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser fileChooser = new JFileChooser(); if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { // Get the selected file // Get the selected file java.io.File file = fileChooser.getSelectedFile(); jtfInputFile.setText(file.toString()); } } }); } public void splitFile(String filename, int numberOfPieces) { try { BufferedInputStream input = new BufferedInputStream( new FileInputStream(new File(filename))); for (int i = 1; i