博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
文件流复习
阅读量:6633 次
发布时间:2019-06-25

本文共 1089 字,大约阅读时间需要 3 分钟。

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);
}
}
}
}

转载于:https://www.cnblogs.com/DJYBlog/p/4107617.html

你可能感兴趣的文章