Results 1 to 2 of 2
  1. #1
    Trithan is offline Member
    Join Date
    Aug 2012
    Posts
    1
    Rep Power
    0

    Default Problem with File I/O, writing information to a binary file.

    Okay, so I need to write information to a binary file, and while I have my window completely set up and laid out like I want it, when I input the information into the boxes and press write record it does nothing. In our example code, the information is written to an example .dat file and is cleared from the text boxes. Mine does nothing.

    Also, I wanted to know how to write a String (in this case the employee name) to my binary .dat file...I can't figure it out for the life of me.

    Here's the code I have now:

    Java Code:
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class CreatePayrollFile extends JFrame implements ActionListener, WindowListener {
    
    	public static final int WIDTH = 400;
        public static final int HEIGHT = 300;
        JPanel titlePanel = new JPanel();
        JPanel displayPanel = new JPanel(new GridLayout(6, 1));
        JPanel dPanel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JPanel dPanel2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JPanel dPanel3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JPanel dPanel4 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JPanel dPanel5 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JPanel dPanel6 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JPanel dPanel7 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JPanel buttonPanel = new JPanel();
        private JLabel companyName = new JLabel("Payroll Inc.");
        Font bigFont = new Font("Helvetica", Font.ITALIC, 24);
        private JLabel prompt = new JLabel("Enter Payroll Information");
        private JTextField name = new JTextField(10);
        private JTextField number = new JTextField(6);
        private JTextField hourlyRate = new JTextField(4);
        private JTextField regularHours = new JTextField(4);
        private JTextField overtimeHours = new JTextField(4);
        private JLabel iLabel = new JLabel("Employee Name  ");
        private JLabel kLabel = new JLabel("Employee Number  ");
        private JLabel qLabel = new JLabel("Hourly Rate   ");
        private JLabel rLabel = new JLabel("Regular Hours    ");
        private JLabel sLabel = new JLabel("Overtime Hours    ");
        private JButton enterDataButton = new JButton("Enter Data");
        DataOutputStream ostream;
    
        public CreatePayrollFile() {
            super("Create Payroll File - Barrick");
    
            setSize(WIDTH, HEIGHT);
            try {
                ostream = new DataOutputStream(new FileOutputStream("payroll.dat"));
            } catch (IOException e) {
                System.err.println("File not opened");
                System.exit(1);
            }
            Container contentPane = getContentPane();
            contentPane.setLayout(new BorderLayout());
    
            companyName.setFont(bigFont);
            titlePanel.add(companyName);
            titlePanel.setBackground(Color.white);
    
            dPanel1.add(prompt);
            displayPanel.add(dPanel1);
    
            dPanel2.add(iLabel);
            dPanel2.add(name);
            displayPanel.add(dPanel2);
    
            dPanel3.add(kLabel);
            dPanel3.add(number);
            displayPanel.add(dPanel3);
            
            dPanel4.add(qLabel);
            dPanel4.add(hourlyRate);
            displayPanel.add(dPanel4);
            
            dPanel5.add(rLabel);
            dPanel5.add(regularHours);
            displayPanel.add(dPanel5);
            
            dPanel6.add(sLabel);
            dPanel6.add(overtimeHours);
            displayPanel.add(dPanel6);
            
            
            
    
            buttonPanel.setBackground(Color.white);
            buttonPanel.setLayout(new FlowLayout());
            enterDataButton.setMnemonic(KeyEvent.VK_E);
            buttonPanel.add(enterDataButton);
            enterDataButton.addActionListener(this);
    
            contentPane.add(titlePanel, BorderLayout.NORTH);
            contentPane.add(displayPanel, BorderLayout.CENTER);
            contentPane.add(buttonPanel, BorderLayout.SOUTH);
            addWindowListener(this);
        }
    
        private void writeRecord() {
    
        	int employeeNumber, regHours, overHours;
            double hRate;
            
            try {
            	employeeNumber = Integer.parseInt(number.getText());
            	hRate = Integer.parseInt(hourlyRate.getText());
            	regHours = Integer.parseInt(regularHours.getText());
            	overHours = Integer.parseInt(overtimeHours.getText());
            	ostream.writeInt(employeeNumber);
            	ostream.writeDouble(hRate);
            	ostream.writeInt(regHours);
            	ostream.writeInt(overHours);
            	number.setText("");
            	hourlyRate.setText("");
            	regularHours.setText("");
            	overtimeHours.setText("");
            	
            	
            	
            
            } catch (NumberFormatException e2) {
            	System.err.println("Invalid Number ");
            } catch (IOException e3) {
            	System.err.println("Error Writing File");
            	System.exit(1);
            }
        }
    
        public void actionPerformed(ActionEvent e) {
            writeRecord();
        }
    
        public void windowClosing(WindowEvent e) {
            try {
                ostream.close();
            } catch (IOException e4) {
                System.err.println("File not closed");
                System.exit(1);
            }
    
            System.exit(0);
        }
    
        public void windowClosed(WindowEvent e) {
        }
    
        public void windowDeiconified(WindowEvent e) {
        }
    
        public void windowIconified(WindowEvent e) {
        }
    
        public void windowOpened(WindowEvent e) {
        }
    
        public void windowActivated(WindowEvent e) {
        }
    
        public void windowDeactivated(WindowEvent e) {
        }
    
        public static void main(String[] args) {
            CreatePayrollFile cprf = new CreatePayrollFile();
            cprf.setVisible(true);
        }
    }
    For good measure, here's the example code we were given:

    Java Code:
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class CreateMailOrderFile extends JFrame implements ActionListener, WindowListener {
    
        public static final int WIDTH = 400;
        public static final int HEIGHT = 300;
        JPanel titlePanel = new JPanel();
        JPanel displayPanel = new JPanel(new GridLayout(6, 1));
        JPanel dPanel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JPanel dPanel2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JPanel dPanel3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JPanel dPanel4 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JPanel dPanel5 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JPanel dPanel6 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JPanel dPanel7 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JPanel buttonPanel = new JPanel();
        private JLabel companyName = new JLabel("MailOrders INC.");
        Font bigFont = new Font("Helvetica", Font.ITALIC, 24);
        private JLabel prompt = new JLabel("Enter Order Information");
        private JTextField itemNumber = new JTextField(10);
        private JTextField quantity = new JTextField(4);
        private JLabel iLabel = new JLabel("Item Number");
        private JLabel qLabel = new JLabel("Quantity   ");
        private JButton enterDataButton = new JButton("Enter data");
        DataOutputStream ostream;
    
        public CreateMailOrderFile() {
            super("Create Order File - Assignment 10");
    
            setSize(WIDTH, HEIGHT);
            try {
                ostream = new DataOutputStream(new FileOutputStream("orders.dat"));
            } catch (IOException e) {
                System.err.println("File not opened");
                System.exit(1);
            }
            Container contentPane = getContentPane();
            contentPane.setLayout(new BorderLayout());
    
            companyName.setFont(bigFont);
            titlePanel.add(companyName);
            titlePanel.setBackground(Color.white);
    
            dPanel1.add(prompt);
            displayPanel.add(dPanel1);
    
            dPanel2.add(iLabel);
            dPanel2.add(itemNumber);
            displayPanel.add(dPanel2);
    
            dPanel3.add(qLabel);
            dPanel3.add(quantity);
            displayPanel.add(dPanel3);
    
            buttonPanel.setBackground(Color.white);
            buttonPanel.setLayout(new FlowLayout());
            enterDataButton.setMnemonic(KeyEvent.VK_E);
            buttonPanel.add(enterDataButton);
            enterDataButton.addActionListener(this);
    
            contentPane.add(titlePanel, BorderLayout.NORTH);
            contentPane.add(displayPanel, BorderLayout.CENTER);
            contentPane.add(buttonPanel, BorderLayout.SOUTH);
            addWindowListener(this);
        }
    
        private void writeRecord() {
    
            int numOrder, numQuantity;
            try {
                numOrder = Integer.parseInt(itemNumber.getText());
                numQuantity = Integer.parseInt(quantity.getText());
                ostream.writeInt(numOrder);
                ostream.writeInt(numQuantity);
                itemNumber.setText("");
                quantity.setText("");
    
            } catch (NumberFormatException e2) {
                System.err.println("Invalid number ");
            } catch (IOException e3) {
                System.err.println("Error writing file");
                System.exit(1);
            }
        }
    
        public void actionPerformed(ActionEvent e) {
            writeRecord();
        }
    
        public void windowClosing(WindowEvent e) {
            try {
                ostream.close();
            } catch (IOException e4) {
                System.err.println("File not closed");
                System.exit(1);
            }
    
            System.exit(0);
        }
    
        public void windowClosed(WindowEvent e) {
        }
    
        public void windowDeiconified(WindowEvent e) {
        }
    
        public void windowIconified(WindowEvent e) {
        }
    
        public void windowOpened(WindowEvent e) {
        }
    
        public void windowActivated(WindowEvent e) {
        }
    
        public void windowDeactivated(WindowEvent e) {
        }
    
        public static void main(String[] args) {
            CreateMailOrderFile cmof = new CreateMailOrderFile();
            cmof.setVisible(true);
        }
    }

    Thanks in advance for any help! :D

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Problem with File I/O, writing information to a binary file.

    know how to write a String
    Look at the API doc for the DataOutputStream class. It has some write... methods that take a String as argument.

    Mine does nothing.
    Can you explain? The code wrote out a file for me.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Reading / Writing to a binary file
    By JeremyLin in forum New To Java
    Replies: 7
    Last Post: 06-13-2012, 11:12 PM
  2. Problem with file BufferedReader using binary file.
    By allymoha in forum Advanced Java
    Replies: 6
    Last Post: 12-16-2011, 09:01 AM
  3. Replies: 1
    Last Post: 02-27-2010, 01:29 AM
  4. Replies: 3
    Last Post: 05-09-2009, 11:31 PM
  5. Problem while writing xml file
    By Rajesh Sahu in forum Java Servlet
    Replies: 0
    Last Post: 12-23-2008, 04:53 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •