C#FileStream 和StreamReader结合使用

来源:互联网 发布:淘宝链接转换 编辑:程序博客网 时间:2024/05/23 11:55
using System;


using System.Collections.Generic;


using System.ComponentModel;


using System.Data;


using System.Drawing;


using System.Text;


using System.Windows.Forms;


using System.IO;


namespace WindowsApplication6


{


    public partial class Form1 : Form


    {


        public Form1()


        {


            InitializeComponent();


        }






        private void button3_Click(object sender, EventArgs e)//IO类FileStream处理字节流


        {


            //IO类FileStream处理字节流,采用FileMode.Open将byte通过char转换成文本文件


            //说明:


            //Append:如果文件存在则打开文件,将光标移到文件尾部


            //create打开文件时,如果文件不存在则创建文件,存在则覆盖文件


            //createnew:打开文件时,如果文件不存在则创建文件,存在则抛出异常


            //open打开文件时,如果文件不存在则抛出异常


            //openorcreate存在打开,否则创建


            //truncate打开现有文件,清楚文件的内容,但远文件的创建日期还在






            FileInfo filein = new FileInfo(textBox1.Text);


            if (!filein.Exists)


            {


                MessageBox.Show("没有文件");


                return;


            }


            else


            {


                FileStream fstream = new FileStream(textBox1.Text, FileMode.Open);


                byte[] buf = new byte[filein.Length];


                fstream.Read(buf, 0, buf.Length);


                //字节到字符的装换


                Decoder d = Encoding.UTF8.GetDecoder();


                char[] c = new char[filein.Length];


                d.GetChars(buf, 0, buf.Length, c, 0);


                //字符数组转换字符串 string为引用类型数组


                string s = new string(c);


                textBox2.Text = s;


                fstream.Close();


            }


        }






        private void button5_Click(object sender, EventArgs e) // StreamReader读数据


        {


             // StreamReader 旨在以一种特定的编码输入字符,而 Stream 类用于


             //字节的输入和输出。使用 StreamReader 读取标准文本文件的各行信息。






            //除非另外指定,StreamReader 的默认编码为 UTF-8,而不是当前系统


            //的 ANSI 代码页。UTF-8 可以正确处理 Unicode 字符并在操作系统的本地化版本上提供一致的结果。






           //默认情况下,StreamReader 不是线程安全的。有关线程安全包装的信息






           //Read(array<Char>[]()[], Int32, Int32) 和 Write(array<Char>[]()[], Int32, Int32) 方法


            //重载读取和写入 count 参数指定的字符数。


        }






        private void button6_Click(object sender, EventArgs e)//StreamWriter写数据


        {


            StreamWriter sw = new StreamWriter(textBox1.Text,true, Encoding.UTF8);


            sw.Write(textBox2.Text);


            sw.Close();


        }






        private void button8_Click(object sender, EventArgs e) // FileStream 和StreamReader


        {


            FileStream fstream = new FileStream(textBox1.Text, FileMode.Open,FileAccess.ReadWrite);


            StreamReader sr = new StreamReader(fstream);


            string s = "";


            do


            {


                s += sr.ReadLine();


            }


            while (sr.ReadLine() != null);


            textBox2.Text = s;


            sr.Close();


        }


   }


}
原创粉丝点击