In: Computer Science
Create a Java Swing form that will have two Buttons: (1) the first linked to an event handler to write data from two TextFields on the form to an output file; and (2) the other with an event handler to read data from the same file as input and write output to a binary ObjectOutputStream file.
Hi, here is the solution. Go through the comments in the program. Let me know if you require any help. The program will create two files the in program root directory. output.txt is first file which is created from the data of text fields. ObjectFile.bin is the other one created from the output text created. The Write to Object Output Stream button will be disabled initially. You need to enter some text in textfields and click "Write to File" button first. once the output.txt is created the program will enable the the button.
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; public class Demo { // JFrame private JFrame frame; public Demo() { // JFrame object frame = new JFrame("Demo"); // Text fields JTextField textField1 = new JTextField(30); JTextField textField2 = new JTextField(30); // buttons JButton btnWriteToFile = new JButton("Write to File"); JButton btnWriteToObjectOS = new JButton("Write to Object Output Stream"); // action listener for buttons btnWriteToFile.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent actionEvent) { // File object for file output.txt File file = new File("output.txt"); try { // file write object acquired on file object FileWriter fileWriter = new FileWriter(file); // buffered writer object on file writer object BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); // get text from textfield 1 and write to file bufferedWriter.write(textField1.getText()); //write new line before appending next text bufferedWriter.write("\n"); // get text from textfield 2 and write to file bufferedWriter.write(textField2.getText()); // closing buffered writer bufferedWriter.close(); // notify the user using message box JOptionPane.showMessageDialog(frame,"Data written to output.txt"); // clear text fields textField1.setText(""); textField2.setText(""); } catch (IOException e) { JOptionPane.showMessageDialog(frame,"Error while writing to file"); e.printStackTrace(); } } }); btnWriteToObjectOS.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent actionEvent) { try { // File object on output.txt for reading File file = new File("output.txt"); // File Reader object on file object FileReader fileReader = new FileReader(file); // get buffered reader on file reader BufferedReader bufferedReader = new BufferedReader(fileReader); // FileOutputStream object on new binary file FileOutputStream fileOutputStream = new FileOutputStream("ObjectFile.bin"); // get object output stream object on fileOutputstream ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream); String line; // read line by line from bufferedReader while((line = bufferedReader.readLine())!=null) { // write it to new file using object outputstream objectOutputStream.writeObject(line); } // close file streams bufferedReader.close(); objectOutputStream.close(); JOptionPane.showMessageDialog(frame,"Data written to ObjectFile.bin"); } catch (IOException e) { JOptionPane.showMessageDialog(frame,"Error while writing to file"); e.printStackTrace(); } } }); // set size of window frame.setSize(new Dimension(400,200)); // add layout to the frame frame.getContentPane().setLayout(new FlowLayout()); // add components to frame frame.getContentPane().add(textField1); frame.getContentPane().add(textField2); frame.getContentPane().add(btnWriteToFile); frame.getContentPane().add(btnWriteToObjectOS); } public void show() { frame.setVisible(true); } public static void main(String [] args) { Demo demo = new Demo(); demo.show(); } } |