(一)文本文件操作的基本要求
1)数据存取与文件操作
数据的存取方式:
- 数据库存取->适合大量、关系复杂并有序的数据存取;
- 文件存取->适合大量、关系简答的数据存取,如系统的日志文件等;
文件存取的好处:
- 读取操作方便;
- 文件可以存储在任何介质中;
文件存取的方式:
2)需要实现的功能
写入文件:
写入文件步骤:
- 创建文件流
- 创建写入器
- 以流的方式写入数据
- 关闭写入器
- 关闭文件流
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//引入命名空间
using System.IO;
namespace TextFile
{
public partial class FrmFile : Form
{
public FrmFile()
{
InitializeComponent();
}
//写入文件
private void btnWriteAll_Click(object sender, EventArgs e)
{
//【1】创建文件流
FileStream fs = new FileStream("D:\\myfile\\myfile.txt", FileMode.Create);
//【2】创建写入器
StreamWriter sw = new StreamWriter(fs,Encoding.Default);
//【3】以流的方式写入数据
sw.Write(this.txtContent.Text.Trim());
//【4】关闭写入器
sw.Close();
//【5】关闭文件流
fs.Close();
}
}
}
注意:
- 写入的时候可能会遇到文件不能写入的问题,主要是权限的问题,可以创建一个文件夹,修改文件夹的属性;
读取文件:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//引入命名空间
using System.IO;
namespace TextFile
{
public partial class FrmFile : Form
{
public FrmFile()
{
InitializeComponent();
}
//读取文件
private void btnReadAll_Click(object sender, EventArgs e)
{
//【1】创建文件流
FileStream fs = new FileStream("D:\\myfile\\myfile.txt", FileMode.Open);
//【2】创建读取器
StreamReader sr = new StreamReader(fs, Encoding.Default);
//【3】以流的方式读取数据
this.txtContent.Text = sr.ReadToEnd();
//【4】关闭读取器
sr.Close();
//【5】关闭文件流
fs.Close();
}
}
}
模拟写入系统文件:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//引入命名空间
using System.IO;
namespace TextFile
{
public partial class FrmFile : Form
{
public FrmFile()
{
InitializeComponent();
}
//模拟写入系统日志
private void btnWriteLine_Click(object sender, EventArgs e)
{
//【1】创建文件流(文件模式为:追加)
FileStream fs = new FileStream("D:\\myfile\\myfile.txt", FileMode.Append);
//【2】创建写入器
StreamWriter sw = new StreamWriter(fs);
//【3】以流的方式“逐行”写入数据
sw.WriteLine(DateTime.Now.ToString() + " 文件操作正常!");
//【4】关闭写入器
sw.Close();
//【5】关闭文件流
fs.Close();
}
}
}
删除文件:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//引入命名空间
using System.IO;
namespace TextFile
{
public partial class FrmFile : Form
{
public FrmFile()
{
InitializeComponent();
}
//删除文件
private void btnDel_Click(object sender, EventArgs e)
{
File.Delete(this.txtFrom.Text.Trim());
}
}
}
复制文件:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//引入命名空间
using System.IO;
namespace TextFile
{
public partial class FrmFile : Form
{
public FrmFile()
{
InitializeComponent();
}
//复制文件
private void btnCopy_Click(object sender, EventArgs e)
{
if (File.Exists(this.txtTo.Text.Trim())) //首先判断文件是否存在(如果文件存在,直接复制会出现错误)
{
File.Delete(this.txtTo.Text.Trim());//删除文件
}
File.Copy(this.txtFrom.Text.Trim(), this.txtTo.Text.Trim()); //复制文件
}
}
}
移动文件:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//引入命名空间
using System.IO;
namespace TextFile
{
public partial class FrmFile : Form
{
public FrmFile()
{
InitializeComponent();
}
//移动文件
private void btnRemove_Click(object sender, EventArgs e)
{
//首先判断目标路径文件是否存在(如果文件存在,直接复制会出现错误)
if (File.Exists(this.txtTo.Text.Trim()))
{
File.Delete(this.txtTo.Text.Trim());//删除文件
}
if (File.Exists(this.txtFrom.Text.Trim()))//如果当前文件存在则移动
{
//移动文件
File.Move(this.txtFrom.Text.Trim(), this.txtTo.Text.Trim());
}
else
{
MessageBox.Show("文件不存在!");
}
}
}
}
获取当前目录下的文件:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//引入命名空间
using System.IO;
namespace TextFile
{
public partial class FrmFile : Form
{
public FrmFile()
{
InitializeComponent();
}
//获取当前目录下的文件
private void btnShowAllFiles_Click(object sender, EventArgs e)
{
string[] files = Directory.GetFiles("C:\\Myfiles");
this.txtContent.Clear();
foreach (string item in files)
{
this.txtContent.Text += item + "\r\n";
}
}
}
}
获取指定目录下的所有子目录【文件夹】:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//引入命名空间
using System.IO;
namespace TextFile
{
public partial class FrmFile : Form
{
public FrmFile()
{
InitializeComponent();
}
//获取指定目录下的所有子目录
private void btnShowSubDir_Click(object sender, EventArgs e)
{
string[] dirs = Directory.GetDirectories("C:\\Drivers");
this.txtContent.Clear();
foreach (string item in dirs)
{
this.txtContent.Text += item + "\r\n";
}
}
}
}
注意:想要获取文件夹中的文件夹,需要使用递归的方式;
创建目录:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//引入命名空间
using System.IO;
namespace TextFile
{
public partial class FrmFile : Form
{
public FrmFile()
{
InitializeComponent();
}
//创建目录
private void btnCreate_Click(object sender, EventArgs e)
{
Directory.CreateDirectory("C:\\Myfiles\\newfiles");
}
}
}
删除指定目录下的所有目录和文件:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//引入命名空间
using System.IO;
namespace TextFile
{
public partial class FrmFile : Form
{
public FrmFile()
{
InitializeComponent();
}
//删除指定目录下的所有子目录和文件
private void btnDelAllFiles_Click(object sender, EventArgs e)
{
// Directory.Delete("C:\\Myfiles");//要求目录必须为空
//使用DirectoryInfo对象,可以删除不为空的目录
DirectoryInfo dir = new DirectoryInfo("C:\\Myfiles");
dir.Delete(true);
}
}
}
3)对象的保存
程序运行中的数据,是以对象的形式存在的;
对象的特点:
- 对象“生活”在内存空间中,因此,程序一旦关闭,这些对象也都会被CLR的垃圾回收机制销毁;
- 程序第二次运行的时候,对象会以“全新”的状态出现,无法保留上次对象的运行状态;
- 如果希望第二次运行程序时能“重现”第一次运行对象的“状态”,则应用程序就必须采用某种方式将对象的各个属性的值保存在磁盘文件中,这样在需要运行的时候可以从磁盘文件中重新设置对象的各个属性值,典型的方法是使用文本文件保存对象的各个属性值;
要实现的功能
- 将用户信息封装为对象的属性并保存在文本中;
- 将文本的信息还原成对象的属性并显示出来;
实现效果展示:
保存、读取数据代码实现:
要将数据保存在对象中;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//引入命名空间
using System.IO;
namespace TextFile
{
public partial class FrmFile : Form
{
public FrmFile()
{
InitializeComponent();
}
private void btnSave_Click(object sender, EventArgs e)
{
//封装对象信息,使用对象初始化器,将信息封装在对象中
Student objStu = new Student()
{
Name = this.txtName.Text.Trim(),
Age = Convert.ToInt16(this.txtAge.Text.Trim()),
Gender = this.txtGender.Text.Trim(),
Birthday = Convert.ToDateTime(this.txtBirthday.Text.Trim())
};
//保存到文本文件
FileStream fs = new FileStream("C:\\objStu.obj", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
//一行一行写入文本
sw.WriteLine(objStu.Name);
sw.WriteLine(objStu.Age);
sw.WriteLine(objStu.Gender);
sw.WriteLine(objStu.Birthday.ToShortDateString());
//关闭文件流和写入器
sw.Close();
fs.Close();
}
private void btnRead_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream("C:\\objStu.obj", FileMode.Open);
StreamReader sr = new StreamReader(fs);
//一行一行读取,读取的时候,还是用的初始化器,将读取的信息封装到对象中
Student objStu = new Student()
{
Name = sr.ReadLine(),
Age = Convert.ToInt16(sr.ReadLine()),
Gender = sr.ReadLine(),
Birthday = Convert.ToDateTime(sr.ReadLine())
};
sr.Close();
fs.Close();
//读取到窗体
this.txtName.Text = objStu.Name;
this.txtAge.Text = objStu.Age.ToString();
this.txtGender.Text = objStu.Gender;
this.txtBirthday.Text = objStu.Birthday.ToShortDateString();
}
}
}