写入数据时,应
string filename = Server.MapPath("~/") + this.TextBox2.Text;
FileStream fs = new FileStream(filename, FileMode.Create,
FileAccess.Write, FileShare.None);
StreamWriter sw = new StreamWriter(fs);
sw.Write(this.TextBox1.Text);
sw.Flush();
sw.Close();
fs.Close();
读出时:
string filename = Server.MapPath("~/") + this.TextBox2.Text;
FileStream fs = new FileStream(filename, FileMode.Open,
FileAccess.Read, FileShare.Read);
StreamReader sr = new StreamReader(fs);
this.TextBox1.Text = sr.ReadToEnd();
sr.Close();
fs.Close();
以数据二进制的形式写入数据
string filename = Server.MapPath("~/") + this.TextBox2.Text;
FileStream fs = new FileStream(filename, FileMode.Create,
FileAccess.Write, FileShare.None);
BinaryWriter bw = new BinaryWriter(fs);
string[] data = this.TextBox1.Text.Split(new string[] { " " },
StringSplitOptions.RemoveEmptyEntries);
foreach (string str in data)
{
bw.Write(Convert.ToInt32(str));
}
bw.Close();
fs.Close();
以二进制的形式读出数据
string filename = Server.MapPath("~/") + this.TextBox2.Text;
FileStream fs = new FileStream(filename, FileMode.Open,
FileAccess.Read, FileShare.None);
BinaryReader br = new BinaryReader(fs);
long nums = br.BaseStream.Length / 4;
for (long i = 0; i < nums; i++)
{
this.TextBox1.Text += br.ReadInt32().ToString() + " ";
}
br.Close();
fs.Close();