C# 与文件读写相关的代码

创建文件,写入文件,读取文件内容 等等
File Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
namespace ConFileReadAndWrite
{
    public class FileReadAndWrite
    {
       /// <summary>
       /// 创建文件
       /// </summary>
       /// <param name="path"></param>
        public void CreateFile(string path)
        {
            if (File.Exists(path))
            {
                File.Delete(path);
            }
            File.Create("log.txt");//在运行目录下创建一个log.txt的文件
        }

        /// <summary>
        /// 写入现有的文件中,如果目标不存在则创建文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="content"></param>
        public void WriteFile(string path,string content)
        {
            File.AppendAllText(path, content);
        }

        /// <summary>
        /// 文件复制,用于读取缓存,或者多种格式的文件
        /// </summary>
        /// <param name="originalPath"></param>
        /// <param name="targetPath"></param>
        public void FileCopy(string originalPath,string targetPath)
        {
            //或者使用  File.Copy
            FileStream fs = new FileStream(originalPath, FileMode.Open);
            FileStream fs2 = new FileStream(targetPath, FileMode.Create);
            try
            {
                byte[] data = new byte[1024];
                BufferedStream bs = new BufferedStream(fs);
                BufferedStream bs2 = new BufferedStream(fs2);
                while (fs.Read(data, 0, data.Length) > 0)
                {
                    fs2.Write(data, 0, data.Length);
                    fs2.Flush();
                }
            }
            catch (IOException ex)
            {
                throw ex;
            }
            finally
            {
                fs.Close();
                fs2.Close();
            }
        }

       /// <summary>
        /// 将文件读入文件流中
       /// </summary>
       /// <param name="path"></param>
        public MemoryStream ReadFileStream(string path)
        {
            FileStream fs = new FileStream(path, FileMode.Open);
            byte[] data = new byte[fs.Length];
            fs.Read(data, 0, data.Length);
            fs.Close();
            MemoryStream ms = new MemoryStream(data);
            return ms;//返回内存流 ,比如把文件,以文件流的方式写入内存中 然后从流中拿出来
            //this.pictureBox1.Image =Image.FromStream(ms);
        }

        /// <summary>
        /// 以文本的方式读取文件
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public string ReadFile(string path)
        {
            StreamReader objReader = new StreamReader(path);
            try
            {
                string content = objReader.ReadToEnd();
                return content;
            }
            catch (IOException ex)
            {
                throw ex;
            }
            finally
            {
                objReader.Close();
            }
          
        }

        /// <summary>
        /// 使用 StreamWriter 写入文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="content"></param>
        public void StreamWriterWriteFile(string path, string content)
        {   
            //实例化一个文件流--->与写入文件相关联
            FileStream fs = new FileStream(path, FileMode.Create);
            //实例化一个StreamWriter-->与fs相关联
            StreamWriter sw = new StreamWriter(fs);
            try
            {//开始写入
                sw.Write(content);
            }
            catch (IOException ex)
            {
                throw ex;
            }
            finally
            {
                //清空缓冲区
                sw.Flush();
                //关闭流
                sw.Close();
                fs.Close();
            }
        }


        /// <summary>
        /// 以二进制写入文件,例如创建fqf文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="content"></param>
        public void BinaryWriterWriteFile(string path, string content)
        {
            FileStream fs = new FileStream(path, FileMode.Create);
            //实例化BinaryWriter
            BinaryWriter bw = new BinaryWriter(fs);
            try
            {
                bw.Write(content);
            }
            catch (IOException ex)
            {
                throw ex;
            }
            finally
            {
                //清空缓冲区
                bw.Flush();
                //关闭流
                bw.Close();
                fs.Close();
            }
        }
    }
}

 

posted @ 2013-05-06 10:59  兴想事成  阅读(191)  评论(0编辑  收藏  举报