zoukankan      html  css  js  c++  java
  • c#文件流

     private void btnOpenFile_Click(object sender, EventArgs e)
            {
                //OpenFileDialog   打开要使用的资源
                using (OpenFileDialog ofd=new OpenFileDialog())
                {
                    if (ofd.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                    {
                        MessageBox.Show("请选择文件!");
                        return;
                    }
                    //把文件读取出来  最好不用这种方法
                  // string txt=  File.ReadAllText(ofd.FileName, Encoding.Default);
                    //FileAccess  定义读写的权限
                    //FileMode  指定系统打开文件的方式
                    using (FileStream fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read))
                    {
                        using (StreamReader sr = new StreamReader(fs))
                        {
                            //从控件中一行一行读出
                            //EndOfStream  获取一个值  当值表示当前的流是否在流的末尾
                            while (!sr.EndOfStream)
                            {
                               string line = sr.ReadLine();
                               this.txtFileContent.Text += line;
                            }
                        }
                    }
                }
            }
    
            /// <summary>
            /// 保存文件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnSave_Click(object sender, EventArgs e)
            {
                using (SaveFileDialog sfd=new SaveFileDialog())
                {
                    if (sfd.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                    {
                        return;
                    }
                   // WriteAllText  创建一个新文件  写入指定的字符串 关闭文件  存在覆盖
                    // File.WriteAllText(sfd.FileName, txtFileContent.Text, Encoding.Default);
                    //using (StreamWriter sw=new StreamWriter(sfd.FileName,false,Encoding.Default,1024*1024))
                    //{
                    //    sw.Write(txtFileContent.Text);
                    //    //清理当前缓冲区  并使所有的缓冲数据写入基础流中
                    //    sw.Flush();
                    //}
    
                    //FileMode指定操作系统应打开的文件
                    
    
                    using (FileStream fs = new FileStream(sfd.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite)) 
                    {
                        //替换空格
                        string txt = txtFileContent.Text.Replace("
    ", "
    ");
                        
                        byte[] data = Encoding.Default.GetBytes(txt);
    
                        fs.Write(data, 0, data.Length);
                        
                    }
    
                }
            }
    
  • 相关阅读:
    Vue3小知识
    Eslint小知识
    微信小程序注意点
    vue常用方法2
    vue常用方法
    vue组件常用方法
    013 --TypeScript之高级类型
    012--TypeScript之类型推断
    jenkins window unity 控制台输出中文乱码
    Unity 生成 Android App Bundle(aab) (二)
  • 原文地址:https://www.cnblogs.com/sunliyuan/p/7029419.html
Copyright © 2011-2022 走看看