Sunday, January 24, 2016

Read Write Binary File



package AccountToObject;



import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author zbawk
 */
public class BinaryFileFrame extends javax.swing.JFrame {

    //ArrayList<Accounts> accountsArray = new ArrayList<>();
    ArrayList<Account> accountArr = new ArrayList<>();
   
    NumberFormat nf = NumberFormat.getCurrencyInstance();
    /**
     * Creates new form BinaryFileFrame
     */
    public BinaryFileFrame() {
        initComponents();
    }
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        //Use the DataOutputStream class to write binary data to a file
        //the constructor of this class, needs a FileOutputStream that opens
        //the file for writing or appending
       
        //get filename from user
        String filename = jtxtFileName.getText();
        //create a File object
        File file = new File(filename); //Import java io file
        //use the exists method in the File class to check if this file exists
        if(!file.exists()){ //Sorround statement with try catch
            try {
                //if it does not exists create it
                boolean created = file.createNewFile();
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
                return;
            }
           
        }
        //get data to write to file
        String bankName = jtxtBankName.getText();
        int accountNumber = Integer.parseInt(jtxtAccountNumber.getText());
        double accountBalance = Double.parseDouble(jtxtAccountBalance.getText());
       
        //create a DataOutPutStream
        DataOutputStream  dos = null; //Import java io dataoutputstream
        try {
            dos = new DataOutputStream (new FileOutputStream(filename, true));//Add import java.io file out put stream
           
            dos.writeUTF(bankName);
            dos.writeInt(accountNumber);
            dos.writeDouble(accountBalance);
        }catch (IOException ex){
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }finally {
            if (dos != null)
                try {
                    dos.close();
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            }
        }
    }                                      

    private void jbtnReadAccountsActionPerformed(java.awt.event.ActionEvent evt) {                                                
        // TODO add your handling code here:
        //use the DataInputStream and the FileInputStream
        DataInputStream dis = null; //import java.io.DataInputStream
        try {
            dis = new DataInputStream(new FileInputStream(jtxtFileName.getText())); //Add import java.io.FileInputStream
           
            //Clear the text area
            jTextArea1.setText("");
            //read accounts from file
            while(true){
                String bankName = dis.readUTF();
                int accountNumber = dis.readInt();
                double accountBalance = dis.readDouble();
                //display
                jTextArea1.append(bankName + " " + accountNumber + " " +
                        accountBalance + "\n");
               
            }
        } catch (FileNotFoundException ex) {
           
        } catch (EOFException ex){//Add import for java.io.EOFException
           
        }catch (IOException ex){
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
    }                                

//==============================
//Lab Assignment
//Use this project: Add button to save to an array list
//Add button to write the entire array list to a file
//read the file back to an array list when the frame first loads

No comments:

Post a Comment