• FileStream对文本进行读写操作


        class FileHelper
        {
      
    
            /// <summary>
            /// 检验文件路径是否合法
            /// </summary>
            /// <param name="path">文件路径</param>
            private static bool CheckPath(string path)
            {
                //正确格式:C:UsersjcxDesktopTest.txt
                string pattern = @"w{1}:([\].+)*.+.w{3,}";
                Regex rg = new Regex(pattern);
                return rg.IsMatch(path);
            }
    
            
           /// <summary>
           /// 创建一个新的文本文件
           /// </summary>
            /// <param name="path">文件路径</param>
           /// <param name="content">写入到文本的内容</param>
            public static void CreateNewTxtFile(string path,string content)
            {
                if (!CheckPath(path))
                {
                    throw new Exception("文件路径不合法");
                }
                //存在则删除
                if (File.Exists(path))
                {
                    File.Delete(path);
                } 
    
                using (FileStream fs=new FileStream (path,FileMode.CreateNew,FileAccess.Write))
                {
                    byte[] bt = Encoding.Default.GetBytes(content);
                    fs.Write(bt,0,bt.Length);
    
                } //using
            }
    
         
            /// <summary>
           /// 读取文本文件
           /// </summary>
           /// <param name="path">文件路径</param>
           /// <param name="readByte">指定每次读取字节数</param>
            /// <returns>读取的全部文本内容</returns>
            public static string ReadTxtFile(string path,long readByte)
            {
                if (!CheckPath(path))
                {
                    throw new Exception("文件路径不合法");
                }
    
                StringBuilder result = new StringBuilder();
                using (FileStream fs=new FileStream (path,FileMode.Open,FileAccess.Read))
                {
                    byte[] bt = new byte[readByte];
    
                    while (fs.Read(bt,0,bt.Length)>0) //每次只从文件中读取部分字节数,一点点读
                    {
                        string txt = Encoding.Default.GetString(bt);  //解码转换成字符串
                        result.AppendLine(txt);
                    }
    
                } //using
                return result.ToString();
    
            }
    
    
        }
    
        class Program
        {
            static void Main(string[] args)
            {
    
                string path = @"C:UsersjcxDesktopTest.txt";
    
                FileHelper.CreateNewTxtFile(path, "好好努力");
                string r = FileHelper.ReadTxtFile(path,2);      //2个字节为一个汉字,一个汉字一个汉字的读
                Console.WriteLine(r);
    
            } // Main
    
    
    
        }
  • 相关阅读:
    Windows Forms中通过自定义组件实现统一的数据验证(二)
    The WindowsClient.NET Community Site Launches
    二十六岁,仍在路上
    Visual Studio 2008 Express版本下载
    Page Controller及其在ASP.NET中的实现
    iBATIS In Action:使用映射语句(二)
    在VS2005中创建项目模板来提高开发效率
    2007年,听见春天的脚步
    iBATIS In Action:使用映射语句(一)
    iBATIS In Action:序言和目录
  • 原文地址:https://www.cnblogs.com/green-jcx/p/9425460.html
Copyright © 2020-2023  润新知