一段老师写的,C#文件读取代码例子


using System;
using System.IO;
using System.Collections;
namespace FileOperation
{
    public class TestFile
    {
        public static void MakeDirectory()
        {
            string strPath = Directory.GetCurrentDirectory();
            strPath += "/kk/";
            if (!Directory.Exists(strPath))
            {
                Directory.CreateDirectory(strPath);
            }
            // Console.WriteLine (strPath);
        }
        //创建一个文件夹在目录下
        public static void DeleteDirectory()
        {
            string strPath = Directory.GetCurrentDirectory();
            strPath += "/kk/";
            if (Directory.Exists(strPath))
            {
                Directory.Delete(strPath, true);
            }
        }
        //删除以存在的文件夹
        public static void MoveAndCopyDirectory()
        {
            string strPath = Directory.GetCurrentDirectory();
            strPath += "/kk/TT/";
            string strPath2 = Directory.GetCurrentDirectory();
            strPath2 += "/kk/GT/";
            // Console.WriteLine (strPath);


            Directory.Move(strPath2, strPath);
        }
        //改变文件夹名称
        public static void ChooseTime()
        {
            string strPath = Directory.GetCurrentDirectory();
            strPath += "/kk/";
            if (Directory.Exists(strPath))
            {
                DateTime pTime = Directory.GetCreationTime(strPath);
                Console.WriteLine(pTime.ToString("yyyy/MM/dd hh:mm:ss"));
                pTime = DateTime.Parse("2016/06/16 00:00:00");
                Console.WriteLine(pTime.ToString("yyyy/MM/dd hh:mm:ss"));
            }
        }
        //更改文件创建时间
        public static bool MakeFileEx(string strPath, ref byte[] pBuffer, bool isBinary = true)
        {
            bool isRet = false;
            FileStream pFile = null;
            const int nMaxWriteSize = 65535;
            do
            {
                if (File.Exists(strPath))
                {
                    break;
                }
                if (pBuffer == null || pBuffer.Length <= 0)
                {
                    break;
                }
                pFile = File.Create(strPath, pBuffer.Length, FileOptions.Asynchronous);
                if (pFile == null)
                {
                    break;
                }
                if (isBinary)
                {
                    BinaryWriter pWrite = new BinaryWriter(pFile);
                    if (pWrite == null)
                    {
                        break;
                    }
                    int nWriteCount = 0;
                    long nFileLen = pFile.Length;
                    while (nWriteCount < pBuffer.Length)
                    {
                        pWrite.Write(pBuffer, nWriteCount, nMaxWriteSize > pBuffer.Length - nWriteCount ? pBuffer.Length - nWriteCount : nMaxWriteSize);
                        pWrite.Flush();
                        nWriteCount += (int)(pFile.Length - nFileLen);
                    }
                }
                else
                {
                    int nWriteCount = 0;
                    long nFileLen = pFile.Length;
                    while (nWriteCount < pBuffer.Length)
                    {
                        pFile.Write(pBuffer, nWriteCount, nMaxWriteSize > pBuffer.Length - nWriteCount ? pBuffer.Length - nWriteCount : nMaxWriteSize);
                        pFile.Flush();
                        nWriteCount += (int)(pFile.Length - nFileLen);
                    }
                }
                isRet = true;
            } while (false);
            if (pFile != null)
            {
                pFile.Flush();
                pFile.Close();
            }
            return isRet;
        }


        public static string GetPath(string value)//获取路径
        {
            string strPath = Directory.GetCurrentDirectory();
            if (!value.Substring(0, 1).Equals("/"))
            {
                strPath += "/";
            }
            strPath += value;
            return strPath;
        }


        public static void MakeFile()//文件写入
        {
            string strPath = Directory.GetCurrentDirectory();
            strPath += "/kk/TT";
            FileStream pFile = File.Create((strPath + "/MengBi.txt"), 100, FileOptions.Asynchronous);
            string strBuffer = "这是一个蒙蔽的程序猿蒙蔽的写入";
            byte[] pBuffer = System.Text.Encoding.UTF8.GetBytes(strBuffer);
            pFile.Write(pBuffer, 0, pBuffer.Length);
            pFile.Flush();
            pFile.Close();
        }
        //创建一个文本并写入


        public static bool TestRead()
        {
            string strPath = Directory.GetCurrentDirectory();
            strPath += "/kk/TT/1.txt";
            FileStream pFile = new FileStream(strPath, FileMode.Open);//读取文件
                                                                      //FileStream Mytxt = File.OpenRead(strPath);//第二种读取
            if (pFile == null)
            {
                Console.WriteLine("file open error!");
                return false;
            }
            byte[] pBuffer = new byte[(int)strPath.Length];//建立一个缓存池
            int ReadLen = pFile.Read(pBuffer, 0, pBuffer.Length);//把这个流读进缓冲池中去
            pFile.Close();
            if (ReadLen > 0)
            {
                string A = System.Text.Encoding.UTF8.GetString(pBuffer);
                Console.WriteLine("1.txt:{0}", A);
                return true;
            }
            return false;
        }




        public static bool TestReadEx(string strPath, bool isBinary = true)
        {
            bool isRet = false;
            const int nMaxReadSize = 65535;
            byte[] pBuffer = new byte[nMaxReadSize];
            FileStream m_pReadFile = new FileStream(strPath, FileMode.Open);
            do
            {
                if (!File.Exists(strPath))
                {
                    break;
                }
                if (pBuffer == null || pBuffer.Length <= 0)
                {
                    break;
                }
                if (m_pReadFile == null)
                {
                    break;
                }
                if (isBinary)
                {
                    BinaryReader pRead = new BinaryReader(m_pReadFile);
                    if (pRead == null)
                    {
                        break;
                    }
                    int nReadCount = 0;
                    while (nReadCount < m_pReadFile.Length)
                    {
                        pRead.Read(pBuffer, nReadCount, nMaxReadSize > (int)(m_pReadFile.Length - nReadCount) ? (int)(m_pReadFile.Length - nReadCount) : nMaxReadSize);
                        Console.WriteLine("txt:{0}", System.Text.Encoding.UTF8.GetString(pBuffer));
                        nReadCount += (int)pBuffer.Length;
                    }
                }
                else
                {
                    int nReadCount = 0;
                    while (nReadCount < m_pReadFile.Length)
                    {
                        m_pReadFile.Read(pBuffer, nReadCount, nMaxReadSize > (int)(m_pReadFile.Length - nReadCount) ? (int)(m_pReadFile.Length - nReadCount) : nMaxReadSize);
                        Console.WriteLine("txt:{0}", System.Text.Encoding.UTF8.GetString(pBuffer));
                        nReadCount += (int)pBuffer.Length;
                    }
                }
                isRet = true;
            } while (false);
            return isRet;
        }




    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Unity3d青子

难题的解决使成本节约,求打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值