c# FileStream简单应用及字符转换

        //字符串转换
        byte[] bt1 = new byte[100];
        char[] chars1 = new char[100];
        string strs = string.Empty;

        chars1 = System.Text.Encoding.UTF8.GetChars(bt1);//byte[] 转换为 char[]
        Decoder dc1 = Encoding.UTF8.GetDecoder();
        dc.GetChars(bt1, 0, bt1.Length, chars, 0);//byte[] 转换为 char[]
       
        chars1 = strs.ToArray();//string 转换为 char[]

        bt1 = System.Text.Encoding.UTF8.GetBytes(strs);//string 转换为 byte[]

        strs = new string(chars);//char[] 转换为 string

        strs = System.Text.Encoding.UTF8.GetString(bt1);//byte[] 转换为 string


        bt1 = System.Text.Encoding.UTF8.GetBytes(chars1);//char[] 转换为 byte[]
        Encoder en = Encoding.UTF8.GetEncoder();
        en.GetBytes(chars, 0, chars.Length, bt1, 0, true);//char[] 转换为 byte[]

//FileStream StreamReader StreamWriter
// fs.Position = fs.Length;//设置写入位置为文本末 方法1;
//fs.Seek(0, SeekOrigin.End);//设置写入位置为文本末 方法2;

using System;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace 文件流读写
{
    public partial class Form1 : Form
   {
        public Form1()
       {
            InitializeComponent();
        }
    private string path = System.Environment.CurrentDirectory + "\\test.txt";

    private void button2_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(this.textBox1.Text))
        {
            MessageBox.Show("文本框内容为空,不能写入");
        }
        else
        {
            FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            fs.Position = fs.Length;//设置写入位置为文本末 方法1; 
            //fs.Seek(0, SeekOrigin.End);//设置写入位置为文本末 方法2;

            ////使用StreamWriter写入
            //using (StreamWriter sw = new StreamWriter(fs))
            //{
            //    sw.WriteLine(this.textBox1.Text.ToString());
            //    sw.Flush();
            //    sw.Close();
            //    fs.Close();
            //}

            //使用byte[]写入
            try
            {
                //char[] 转换为byte[] 发法1
                Encoder en = Encoding.UTF8.GetEncoder();//write写入字符编码转换
                byte[] bt1 = new byte[this.textBox1.Text.ToArray().Length];
                char[] chars = this.textBox1.Text.ToArray();
                en.GetBytes(chars, 0, chars.Length, bt1,0, true);
                fs.Write(bt1, 0, bt1.Length);

                ////char[] 转换为byte[] 发法2
                //byte[] bt = Encoding.UTF8.GetBytes(this.textBox1.Text.ToString());
                //fs.Write(bt, 0, bt.Length);

                fs.Flush();
                fs.Close();
            }
            catch (Exception)
            {

                throw;
            }
            
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
        //StreamReader sr= new StreamReader(path)
        //using (StreamReader sr= new StreamReader(fs))
        //{
        //    string temp = sr.ReadLine();
        //    while (temp!=null)
        //    {
        //        this.textBox1.AppendText(temp+"\r\n");
        //        temp = sr.ReadLine();
        //    }

        //    sr.Close();
        //}

       
        //byte[] 转换为char[]方法1
        byte[] bt = new byte[100];
        fs.Read(bt,0,100);
        ////byte[] 转换为char[]方法1
        char[] chars = new char[100];
        Decoder dc = Encoding.UTF8.GetDecoder();
        dc.GetChars(bt, 0, bt.Length, chars, 0);

        ////byte[] 转换为char[]方法2
        //char[] chars = Encoding.UTF8.GetChars(bt);
        ////char[] 转换为string new string(chars)
        ////string 转换为char[]  string.ToArray();
        this.textBox1.AppendText(new string(chars));
    }
}

}

你可能感兴趣的:(c# FileStream简单应用及字符转换)