(); splitStrings = new string[100]; splitStrings1 = new string[100]; splitStrings2 = new string[100]; }private void button1_Click(object sender, EventArgs e) {
if (textBox1.Text == \"\" ) {
MessageBox.Show(\"学号不能为空!\"); }
else if (textBox2.Text == \"\") {
MessageBox.Show(\"姓名不能为空!\"); }
else if (textBox3.Text == \"\") {
MessageBox.Show(\"班级不能为空!\"); }
else if (textBox4.Text == \"\") {
MessageBox.Show(\"专业不能为空!\"); } else {
FileStream fs = new FileStream(\"d://student.txt\", FileMode.OpenOrCreate | FileMode.Append, FileAccess.Write);
StreamWriter streamWriter = new StreamWriter(fs);
streamWriter.WriteLine(\"学号=\" + textBox1.Text + \" \" + \"姓名=\" + textBox2.Text + \" \" + \"班级=\" + textBox3.Text + \" \" + \"专业=\" + textBox4.Text); textBox1.Clear(); textBox2.Clear(); textBox3.Clear(); textBox4.Clear(); streamWriter.Flush(); streamWriter.Close(); }
}
private void button2_Click(object sender, EventArgs e) {
FileStream fs = new FileStream(\"d://student.txt\", FileMode.Open, FileAccess.Read);
第4页
大理学院课程教案(教学内容)
StreamReader streamReader = new StreamReader(fs); this.richTextBox1.Text = \"\";
string strLine = streamReader.ReadLine(); while (strLine != null) {
this.richTextBox1.Text += strLine + \"\\n\"; st = new Student();
splitStrings = strLine.Split(separator);//以空格分隔符拆分字符串 for (int i = 0; i < splitStrings.Length;i++ ) {
splitStrings1 = splitStrings[i].Split(separator1);//以=号分隔符拆分字符串 splitStrings2[i] = splitStrings1[1]; }
st.xh = splitStrings2[0]; st.xm = splitStrings2[1]; st.bj = splitStrings2[2]; st.zy = splitStrings2[3];
stList.Add(st);//还原学生对象并存储在list表里 strLine = streamReader.ReadLine(); }
streamReader.Close();
textBox8.Text = stList[0].xh; textBox7.Text = stList[0].xm; textBox6.Text = stList[0].bj; textBox5.Text = stList[0].zy; }
private void button3_Click(object sender, EventArgs e) {
if((stList.ToArray()).Length<=0) {
MessageBox.Show(\"当前记录为0条\"); }
if (i == 0&&(stList.ToArray().Length>0)) {
textBox8.Text = stList[0].xh; textBox7.Text = stList[0].xm; textBox6.Text = stList[0].bj; textBox5.Text = stList[0].zy; }
if ((i > 0) && (i < stList.ToArray().Length)) {
i = i - 1;
textBox8.Text = stList[i].xh; textBox7.Text = stList[i].xm; textBox6.Text = stList[i].bj; textBox5.Text = stList[i].zy; } }
private void button4_Click(object sender, EventArgs e) {
if ((stList.ToArray()).Length <= 0) {
MessageBox.Show(\"当前记录为0条\"); }
if (i == (stList.ToArray()).Length&&i!=0) {
第5页
大理学院课程教案(教学内容)
textBox8.Text = stList[i].xh; textBox7.Text = stList[i].xm; textBox6.Text = stList[i].bj; textBox5.Text = stList[i].zy; }
if (i >= 0 && i < (stList.ToArray()).Length-1) {
i = i + 1;
textBox8.Text = stList[i].xh; textBox7.Text = stList[i].xm; textBox6.Text = stList[i].bj; textBox5.Text = stList[i].zy; } } } }
学生类定义如下:
using System;
using System.Collections.Generic; using System.Linq; using System.Text;
namespace 读写文件 {
public class Student {
public string xh; public string xm; public string bj;
public string zy;//同学把这些变量改为属性定义 } }
2.参考上述程序,编写一个完成职工信息IO操作的程序。职工信息存储在D盘的Staff文件夹下,文件名为Staff.txt;职工信息包括:编号,姓名,性别,毕业学校,学历,学位,工作部门,联系电话,邮箱地址,住址等。实现职工信息的存储、读取,浏览。
第6页
大理学院课程教案(教学总结)
第7页