zoukankan      html  css  js  c++  java
  • 文件流FileStream案例二

    先上代码 

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO;
    
    namespace 文件流FileStream案例二
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            int TypeW = 0;
            /// <summary>
            /// 选定某个文件夹
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button1_Click(object sender, EventArgs e)
            {
                FolderBrowserDialog openfolder = new FolderBrowserDialog();
                if (openfolder.ShowDialog() == DialogResult.OK)
                {
                    textBox1.Text = Convert.ToString(openfolder.SelectedPath);
                    TypeW = 1;
                }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                OpenFileDialog openfile = new OpenFileDialog();
                openfile.Filter = "文本文件|*.txt";
                if (openfile.ShowDialog() == DialogResult.OK)
                {
                    FileStream openfilestream = new FileStream(openfile.FileName, FileMode.Open, FileAccess.Read);
                    StreamReader sr = new StreamReader(openfilestream, Encoding.Default);
                    richTextBox1.Text = sr.ReadToEnd();
                    textBox2.Text = Convert.ToString(openfile.FileName);
                    sr.Close();
                    openfilestream.Close();
                    TypeW = 2;
                }
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                if (richTextBox1.Text == string.Empty)
                {
                    MessageBox.Show("编辑文本文件内容禁止为空", "提示信息");
                    return;
                }
                else
                {
                    if (TypeW == 1)
                    {
                        FileStream fs = new FileStream(textBox1.Text + @"\anshenwoaini.txt", FileMode.Create, FileAccess.ReadWrite);
                        StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                        sw.Write(richTextBox1.Text);
                        TypeW = 0;
                        MessageBox.Show("已经成功地将文本文件写入");
    
                        sw.Close();
                        fs.Close();
                    }
                    else if (TypeW == 2)
                    {
                        FileStream fs = new FileStream(textBox2.Text, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                        StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                        sw.Write(richTextBox1.Text);
                        MessageBox.Show("yijingchenggong");
                        sw.Close();
                        fs.Close();
                    }
                }
            }
        }
    }
    View Code

    首先,选定文件夹是为了选择新文本文件保存路径。

          这是提示用户选择文件

    SelectedPath是FolderBrowserDialog类的一个方法,获取或设置用户选定的路径。

    选定文件是打开已存在的文件进行继续编辑。

    在一般情况下,写不写FileStream都可以进行,但是

    所以

    不管是读还是写,StreamReader和StreamWriter使用前用文件流FileStream来读,防止数据丢失。

    最后要根据先用后关的原则,跟栈一样,先进后出地关闭线程资源,顺序不能错。

    保存文件,要根据上面两种情况分类,写法类似。

  • 相关阅读:
    python求pi的方法
    Python:字符串格式化
    Python time模块学习
    开源的PaaS平台
    车牌识别技术实现方式及应用场景
    ASP.NET车辆管理系统
    Spark+Hadoop+IDE环境搭建
    大数据平台技术方案及案例
    主流大数据平台及解决方案对比
    大数据平台架构——通用版
  • 原文地址:https://www.cnblogs.com/zjx123/p/8629877.html
Copyright © 2011-2022 走看看