文件流复习

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 _02文件流复习
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
//使用File静态类实现写入
// File.WriteAllText(textBox1.Text.Trim(), textBox2.Text.Trim());
//使用FileStream类实现写入数据
using (FileStream fsWrite = File.OpenWrite(textBox1.Text.Trim()))
{
//1.把字符串转换为byte[]
byte[] buffer = Encoding.UTF8.GetBytes(textBox2.Text.Trim());

//写入数据
fsWrite.Write(buffer, 0, buffer.Length);
}
MessageBox.Show("ok");
}

private void button2_Click(object sender, EventArgs e)
{
//1.通过File来实现读取
//textBox2.Text = File.ReadAllText(textBox1.Text.Trim());

//2.通过FileStream实现读取
using (FileStream fsRead = File.OpenRead(textBox1.Text.Trim()))
{
byte[] buffer = new byte[fsRead.Length];
fsRead.Read(buffer, 0, buffer.Length);
textBox2.Text = Encoding.UTF8.GetString(buffer);
}
}
}
}

你可能感兴趣的:(文件流)