fileStream操作

来源:互联网 发布:王者趋势为王指标源码 编辑:程序博客网 时间:2024/06/08 23:14


using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO;namespace FileStreamForm{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void button2_Click(object sender, EventArgs e)        {            FileStream fs;            try            {                fs = File.Create(fname.Text);            }            catch            {                MessageBox.Show("建立文件时出错!", "错误", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);                return;            }            byte[] content = new UTF8Encoding(true).GetBytes(this.richTextBox1.Text);            try            {                fs.Write(content, 0, content.Length);                fs.Flush();                MessageBox.Show("保存成功");            }            catch            {                MessageBox.Show("文件写入错误");            }            finally            {                fs.Close();            }        }        private void button1_Click(object sender, EventArgs e)        {            string path = fname.Text;            UTF8Encoding temp = new UTF8Encoding(true);            FileStream fs;            try            {                fs = new FileStream(path, FileMode.Open, FileAccess.Read);            }            catch {                MessageBox.Show("建议文件出错");                return;            }            byte[] b = new byte[fs.Length];            try            {                fs.Read(b, 0, (int)fs.Length);                richTextBox1.Text = temp.GetString(b);            }            catch            {                MessageBox.Show("读取文件出错");            }            finally            {                fs.Close();            }        }    }}



0 0
原创粉丝点击