Stream流读写文件:
private void button3_Click(object sender, EventArgs e)
{
//将textBox3.Text写入nihao.txt文件
Stream s = new FileStream("nihao.txt",FileMode.Create,FileAccess.Write);
{
//将textBox3.Text写入nihao.txt文件
Stream s = new FileStream("nihao.txt",FileMode.Create,FileAccess.Write);
//打开模式,访问方式
StreamWriter sw = new StreamWriter(s, Encoding.Default);//创建Stream流,指定编码方式
sw.Write(textBox3.Text);
sw.Close();
}
StreamWriter sw = new StreamWriter(s, Encoding.Default);//创建Stream流,指定编码方式
sw.Write(textBox3.Text);
sw.Close();
}
private void button2_Click(object sender, EventArgs e)
{
//将nihao.txt文件读出到textBox3.Text
Stream s = new FileStream("nihao.txt", FileMode.Open);
StreamReader sr = new StreamReader(s,Encoding.Default);
textBox3.Text= sr.ReadToEnd().ToString();
sr.Close();
s.Close();
//sr.BaseStream的Position或Seek()可移动文件流指针到的任意位置。
}
{
//将nihao.txt文件读出到textBox3.Text
Stream s = new FileStream("nihao.txt", FileMode.Open);
StreamReader sr = new StreamReader(s,Encoding.Default);
textBox3.Text= sr.ReadToEnd().ToString();
sr.Close();
s.Close();
//sr.BaseStream的Position或Seek()可移动文件流指针到的任意位置。
}
二进制文件的读写:
private void button5_Click(object sender, EventArgs e)
{
Stream s = new FileStream("nihao1.txt", FileMode.Create);
BinaryWriter sw = new BinaryWriter(s, Encoding.Unicode);
sw.Write(textBox3.Text);
sw.Close();
}
{
Stream s = new FileStream("nihao1.txt", FileMode.Create);
BinaryWriter sw = new BinaryWriter(s, Encoding.Unicode);
sw.Write(textBox3.Text);
sw.Close();
}
private void button6_Click(object sender, EventArgs e)
{
// MessageBox.Show(Convert.ToChar(97).ToString());
Stream s = new FileStream("nihao1.txt", FileMode.Open);
BinaryReader sr = new BinaryReader(s, Encoding.Unicode);
textBox3.Text=sr.ReadString().ToString();
sr.Close();
s.Close();
}
{
// MessageBox.Show(Convert.ToChar(97).ToString());
Stream s = new FileStream("nihao1.txt", FileMode.Open);
BinaryReader sr = new BinaryReader(s, Encoding.Unicode);
textBox3.Text=sr.ReadString().ToString();
sr.Close();
s.Close();
}