Thursday, October 2, 2014

BinaryReaderWriter


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;



namespace BinaryReaderWriter
{
    class Car
    {
        private string _make;
        private string _model;
        private int _year;
        private int _mileage;
        private decimal _price;

        public Car(string make, string model, int year, int mileage, decimal price)
        {
            _make = make;
            _model = model;
            _year = year;
            _mileage = mileage;
            _price = price;
        }

        public string Make
        { get { return _make; } }
        public string Model
        { get { return _model; } }
        public int Year
        { get { return _year; } }
        public int Mileage
        { get { return _mileage; } }
        public decimal Price
        { get { return _price; } }
    }
}
//=============================================================

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO; //add to create open file

namespace BinaryReaderWriter
{
    public partial class Zau : Form
    {
        string filepath = "cardata.dat";

        List<Car> carList = new List<Car>();
        public Zau()
        {
            InitializeComponent();
        }

        private void btnSaveToFile_Click(object sender, EventArgs e)
        {
            //Need a FileStream object to open/create the file
            //BinaryWriter to write to a binary file
            //BinaryReader to read a binary file
            FileStream fs = null;
            BinaryWriter br = null;
            try
            {
                fs = new FileStream(filepath, FileMode.Append, FileAccess.Write);
                //1. open file for writing or appending
                br = new BinaryWriter(fs);
                //2. write object data to the file

                string make = txtMake.Text;
                string model = txtModel.Text;
                int year = int.Parse(txtYear.Text);
                int mileage = int.Parse(txtMileage.Text);
                decimal price = decimal.Parse(txtPrice.Text);

                //save
                br.Write(make);
                br.Write(model);
                br.Write(year);
                br.Write(mileage);
                br.Write(price);

                MessageBox.Show("Data has been saved", "Car data", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (FormatException fe)
            {
                MessageBox.Show(fe.Message);
            }
            catch (ArgumentException ae)
            {
                MessageBox.Show(ae.Message);
            }
            catch (IOException ioe)
            {
                MessageBox.Show(ioe.Message);
            }

            finally
            {
                //3. close file
                if (br != null) br.Close();
                if (fs != null) fs.Close();
            }
           
        }
        //Read car data
        private void btnReadCarData_Click(object sender, EventArgs e)
        {
            //read binary file
            //need to use FileStream to open the file for reading
            //need to use BinaryReader to read binary data from file
            FileStream fs = null;
            BinaryReader br = null;

            try
            {
                fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);

                br = new BinaryReader(fs);

                listView1.Items.Clear();

                while (br.PeekChar() != -1)
                {
                    string make = br.ReadString();
                    string model = br.ReadString();
                    int year = br.ReadInt32();
                    int mileage = br.ReadInt32();
                    decimal price = br.ReadDecimal();

                    //save to a listView

                    //create a listViewItem
                    ListViewItem lvitem = new ListViewItem(make);
                    lvitem.SubItems.Add(model);
                    lvitem.SubItems.Add(year.ToString());
                    lvitem.SubItems.Add(mileage.ToString("n0"));
                    lvitem.SubItems.Add(price.ToString("c"));

                    //add lvitem to the ListView
                    listView1.Items.Add(lvitem);
                }
            }
            catch (FormatException fe)
            {
                MessageBox.Show(fe.Message);
            }
            catch (ArgumentException ae)
            {
                MessageBox.Show(ae.Message);
            }
            catch (IOException ioe)
            {
                MessageBox.Show(ioe.Message);
            }

            finally
            {
                if (fs != null) fs.Close();
                if (br != null) br.Close();
            }
        }

    }
}

No comments:

Post a Comment