wsFile

using System;
using System.Collections.ObjectModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;


namespace wsControls.IO
{
    public class wsFile
    {
        public const char tab = (char)178;
        public const string space = "\t";
        public const char eof = (char)13;
        public const byte CR = 13;
        public const string DateFmt = "yyyy-MM-dd HH:mm:ss.ff";

        public const int MaxHeaderLength = 1024;

        #region [ Text ] AppendText, WriteFile 
        public static bool AppendText(string filename, string text)
        {
            try
            {
                System.IO.StreamWriter file = File.AppendText(filename);
                file.WriteLine(text);
                file.Flush();
                file.Close();
                file.Dispose();
                return true;
            }
            catch { return false; }
        }

        public static bool WriteFile(string filename, string text)
        {
            try
            {
                System.IO.StreamWriter file = new System.IO.StreamWriter(filename);
                file.WriteLine(text);
                file.Close();
                file.Flush();
                file.Dispose();
                return true;
            }
            catch { return false; }
        }
        #endregion

        public static bool WriteBuffer(string filename, byte[] buffer)
        {
            try
            {
                BinaryWriter bw = new BinaryWriter(System.IO.File.Open(filename, FileMode.Create), new UTF8Encoding());
                bw.Write(buffer);
                bw.Close();
                return true;
            }
            catch { return false; }
        }

        #region [  Write BXF ] CreateBXF, AppendBXF, CloseBXF 
        public static bool CreateBXF(string filename, out BinaryWriter bw)
        {
            bw = null;
            try
            {
                bw = new BinaryWriter(System.IO.File.Open(filename, FileMode.Create), new UTF8Encoding());
                return true;
            }
            catch { return false; }
        }

        public static bool AppendBXF(BinaryWriter bw, string text)
        {
            try
            {
                bw.Write(text);
                return true;
            }
            catch { return false; }
        }

        public static bool AppendBXF(BinaryWriter bw, decimal data)
        {
            try
            {
                bw.Write(data);
                return true;
            }
            catch { return false; }
        }

        public static bool AppendBXF(BinaryWriter bw, string text, decimal data)
        {
            try
            {
                bw.Write(text);
                bw.Write(data);
                return true;
            }
            catch { return false; }
        }

        public static bool AppendBXF(BinaryWriter bw, DateTime datetime, decimal data)
        {
            try
            {
                bw.Write(datetime.ToString(DateFmt));
                bw.Write(data);
                return true;
            }
            catch { return false; }
        }

        public static bool AppendBXF(BinaryWriter bw, string[] text, decimal[] data)
        {
            try
            {
                for (int i = 0; i < text.Length; i++)
                {
                    bw.Write(text[i]);
                    bw.Write(data[i]);
                }
                return true;
            }
            catch { return false; }
        }

        public static bool AppendBXF(BinaryWriter bw, float[] data)
        {
            try
            {
                bw.Write(data.Length.ToString());
                for (int i = 0; i < data.Length; i++)
                    bw.Write(data[i]);
                return true;
            }
            catch { return false; }
        }


        public static bool AppendBXF(BinaryWriter bw, string text, float[] data)
        {
            try
            {
                bw.Write(text);
                bw.Write(data.Length.ToString());
                for (int i = 0; i < data.Length; i++)
                    bw.Write(data[i]);
                return true;
            }
            catch { return false; }
        }

        public static bool AppendBXF(BinaryWriter bw, DateTime datetime, float[] data)
        {
            try
            {
                bw.Write(datetime.ToString(DateFmt));
                bw.Write(data.Length.ToString());
                for (int i = 0; i < data.Length; i++)
                    bw.Write(data[i]);
                return true;
            }
            catch { return false; }
        }

        public static bool AppendBXF(BinaryWriter bw, DateTime datetime, decimal[] data)
        {
            try
            {
                bw.Write(datetime.ToString(DateFmt));
                bw.Write(data.Length.ToString());
                for (int i = 0; i < data.Length; i++)
                    bw.Write(data[i]);
                return true;
            }
            catch { return false; }
        }

        public static bool CloseBXF(BinaryWriter bw)
        {
            try
            {
                bw.Flush();
                bw.Close();
                bw.Dispose();
                return true;
            }
            catch { return false; }
        }
        #endregion

        #region [ Read BXF ] OpenBXF, ReadBXF, CloseBXF 
        public static bool OpenBXF(string filename, out FileStream fs, out BinaryReader br)
        {
            fs = null;
            br = null;
            try
            {
                fs = new FileStream(filename, FileMode.Open);
                br = new BinaryReader(fs, new UTF8Encoding());
                return true;
            }
            catch { return false; }
        }

        public static bool ReadBXF(BinaryReader br, out string text)
        {
            try
            {
                text = br.ReadString();
                return true;
            }
            catch
            {
                text = string.Empty;
                return false;
            }
        }

        public static bool ReadBXF(BinaryReader br, out decimal dec)
        {
            try
            {
                dec = br.ReadDecimal();
                return true;
            }
            catch
            {
                dec = 0;
                return false;
            }
        }

        public static bool ReadBXF(BinaryReader br,out string text, out decimal dec)
        {
            try
            {
                text = br.ReadString();
                dec = br.ReadDecimal();
                return true;
            }
            catch
            {
                text = string.Empty;
                dec = 0;
                return false;
            }
        }

        public static bool ReadBXF(BinaryReader br, out DateTime datetime, out decimal dec)
        {
            try
            {
                datetime = Convert.ToDateTime(br.ReadString());
                dec = br.ReadDecimal();
                return true;
            }
            catch
            {
                datetime = new DateTime();
                dec = 0;
                return false;
            }
        }

        public static bool ReadBXF(BinaryReader br, out float[] data)
        {
            try
            {
                string len = br.ReadString();
                data = new float[Convert.ToInt32(len)];
                for (int i = 0; i < data.Length; i++)
                    data[i] = br.ReadSingle();
                return true;
            }
            catch
            {
                data = null;
                return false;
            }
        }

        
        public static bool ReadBXF(BinaryReader br, out string text, out float[] data)
        {
            try
            {
                text = br.ReadString();
                string len = br.ReadString();
                data = new float[Convert.ToInt32(len)];
                for (int i = 0; i < data.Length; i++)
                    data[i] = br.ReadSingle();
                return true;
            }
            catch
            {
                text = string.Empty;
                data = null;
                return false;
            }
        }


        public static bool ReadBXF(BinaryReader br, out DateTime datetime, out float[] data)
        {
            try
            {
                datetime = Convert.ToDateTime(br.ReadString());
                string len = br.ReadString();
                data = new float[Convert.ToInt32(len)];
                for (int i = 0; i < data.Length; i++)
                    data[i] = br.ReadSingle();
                return true;
            }
            catch
            {
                datetime = new DateTime();
                data = null;
                return false;
            }
        }

        public static bool ReadBXF(BinaryReader br, out DateTime datetime, out decimal[] data)
        {
            try
            {
                datetime = Convert.ToDateTime(br.ReadString());
                string len = br.ReadString();
                data = new decimal[Convert.ToInt32(len)];
                for (int i = 0; i < data.Length; i++)
                    data[i] = br.ReadDecimal();
                return true;
            }
            catch
            {
                datetime = new DateTime();
                data = null;
                return false;
            }
        }


        public static bool CloseBXF(FileStream fs, BinaryReader br)
        {
            try
            {
                br.Close();
                fs.Close();
                return true;
            }
            catch { return false; }
        }
        #endregion

        public static bool WriteTXT(string filename, string content)
        {
            try
            {
                System.IO.StreamWriter sfile = new System.IO.StreamWriter(filename);
                sfile.WriteLine(content);
                sfile.Close();
                sfile.Dispose();
                return true;
            }
            catch { return false; }
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值