您的位置:首页 > 编程语言 > C#

C#读写文件

2015-08-17 16:29 543 查看
//下面是读写文件的类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace WriteRead
{
    class FileC
    {
#region Write TO File . Params:path and content
        public static void writeFileStream(string path,string content)
        {
            byte [] date=System.Text.Encoding.Default.GetBytes(content);
            try
            {
                FileStream file = new FileStream(path, FileMode.Append);
                file.Write(date, 0, date.Length);
                file.Flush();
                file.Close();
            }
            catch (Exception)
            {
                
                throw;
            }
            
        }
        /*先使用fileStream创建文件,然后使用StreanWrite 写进文件
        * 
        * 
        */
        public static void writeStreamWrite(string path, string content)
        {
            try
            {
                FileStream file = new FileStream(path, FileMode.Append);
                //创建一个StreamWrite 实例进行输入
                StreamWriter sw = new StreamWriter(file);
                //写入文件
                sw.Write(content);
                //关闭缓冲区。把缓冲区中的数据写入文件
                sw.Flush();
                sw.Close();
                file.Close();
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        /*
         * 使用Bianary写文件
         * 以二进制的形式写文件
         */
        public static void writeBinaryWriter(string path, string content)
        {
            try
            {
                FileStream file = new FileStream(path,FileMode.Append);
                BinaryWriter bw = new BinaryWriter(file);
                bw.Write(content);
                bw.Flush();
                bw.Close();

            }
            catch (Exception)
            {
                
                throw;
            }
        }
#endregion
#region read from file params:path 
        /*
         * 使用fileStream读取文件return byte[]
         */
        public static byte[] readFile(string path)
        { 
            byte[] data;
            try
            {
                //FileStream 构造函数有14个重载,其中常用的就是这个,下面这个还可以读写的权限。
               // FileStream file1=new FileStream (path, FileMode.Open,FileAccess.ReadWrite)
                FileStream file = new FileStream(path, FileMode.Open);
                data = new byte[file.Length];
                file.Read(data, 0, data.Length);
                file.Close();
                return data;
            }
            catch (Exception)
            {
                return null;
                throw;
            }
        }
        /*
         * 以字节流的方式读文件
         * 使用StreamReader读取文件return byte[]
         */
        public static string readStreamReader(string path)
        {
            string data=string.Empty;
            try
            {
                FileStream file = new FileStream(path, FileMode.Open);
                file.Seek(0, SeekOrigin.Begin);
                StreamReader sr = new StreamReader(file);
                string strPrem=string.Empty;
                while ((strPrem=sr.ReadLine())!=null)
                {
                    data += strPrem;
                }
                sr.Close();
                file.Close();
                return data;
            }
            catch (Exception)
            {
                return null;
                throw;
            }
        }
        /*
         * 以二进制的方式读文件
         * 使用BinaryReader读取文件return byte[]
         */
        public static byte[] readBinaryReader(string path)
        {
            try
            {
                FileStream file = new FileStream(path, FileMode.Open);
                file.Seek(0, SeekOrigin.Begin);
                //一般使用二进制方式读文件,因为不受限制,是什么类型的文件都可以读进来
                BinaryReader br = new BinaryReader(file);
                byte [] data=new byte [file.Length];
                br.Read(data, 0, data.Length);
                br.Close();
                file.Close();
                return data;
            }
            catch (Exception)
            {
                return null;
                throw;
            }
        }

#endregion
    }
}

下面是测试类的主程序:

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

namespace WriteRead
{
    class Program
    {
        static void Main(string[] args)
        {
            //TestFileStreamWrite();
            //TestStreamWriter();
            //TestBinaryWriter();
           // Console.WriteLine("write success!");
           // Testread();
           // TestStreamReader();
            //TestBinaryReader();
            Console.ReadKey();
             
        }
#region Test read from file
        public static void Testread()
        {
            string strPath = "E:\\t.txt";
            byte[] data = FileC.readFile(strPath);
            String strcontent = System.Text.Encoding.Default.GetString(data);
            Console.WriteLine(strcontent);
        }
        public static void TestStreamReader()
        {
            string strPath = "E:\\t.txt";
            
            Console.WriteLine(FileC.readStreamReader(strPath));
        }
        public static void TestBinaryReader()
        {
            string strPath = "E:\\t.txt";
            byte[] data = FileC.readBinaryReader(strPath);
            String strcontent = System.Text.Encoding.Default.GetString(data);
            Console.WriteLine(strcontent);
            Console.WriteLine();
        }
        #endregion
        #region Test Write Class
        public static  void TestFileStreamWrite()
        {
            string strContent = "This is a TXT document";
            string strPath="E:\\t.txt";
            FileC.writeFileStream(strPath, strContent);
        }
        public static void TestStreamWriter()
        {
            string strContent = "This is a TestStreamWriter document";
            string strPath = "E:\\t.txt";
            FileC.writeStreamWrite(strPath, strContent);
        }
        public static void TestBinaryWriter()
        {
            string strContent = "This is a TestBinaryWriter document";
            string strPath = "E:\\t.txt";
            FileC.writeBinaryWriter(strPath, strContent);
        }
        #endregion
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: