C#读写二进制文件
时间:2011-02-07 来源:与时俱进
/// <summary>
/// 读取信息方法
/// </summary>
/// <returns>读取是否成功</returns>
public void ReadInfo(List<Student> stu)
{
Console.WriteLine("请输入文件读取路径:(键入回车为默认路径)");
String filename = Console.ReadLine();
FileStream fs;
//默认路径
if (filename == "")
{
fs = new FileStream("student.dll", FileMode.Open);
}
else
{
//如果文件不存在,就提示错误
if (!File.Exists(filename))
{
Console.WriteLine("\n\t读取失败!\n错误原因:可能不存在此文件");
return;
}
//否则创建文件
fs = new FileStream(filename, FileMode.Open);
}
//使用二进制读取
BinaryReader br = new BinaryReader(fs);
Console.Write("读取信息将覆盖现有的信息,继续吗?y/n :");
String command = Console.ReadLine();
if (command == "y" || command == "Y")
{
for (int i = 0; i < stu.Count; i++)
{
stu.RemoveAt(i);
}
//从磁盘上读取信息
try
{
while (true)
{
Student student = new Student();
student.Id = br.ReadString();
student.Name = br.ReadString();
student.Score1 = br.ReadDouble();
student.Score2 = br.ReadDouble();
student.Score3 = br.ReadDouble();
stu.Add(student);
student = null;
}
}
catch (Exception)
{
Console.WriteLine("\n\n读取结束!");
}
}
br.Close();
fs.Close();
}
下面是我的写入方法,写入参数List<Student> stu中的数据
/// <summary>
/// 写入信息方法
/// </summary>
/// <returns>写入是否成功</returns>
public void WriteInfo(List<Student> stu)
{
Console.WriteLine("请输入文件保存路径:(键入回车为默认路径)");
FileStream fs;
String filename = Console.ReadLine();
//默认路径
if (filename == "")
{
fs = new FileStream("student.dll", FileMode.Create);
}
//手动输入路径
else
{
//如果文件存在,就提示错误
if (File.Exists(filename))
{
Console.WriteLine("\n\t保存失败!\n错误原因:可能存在相同文件");
return;
}
//否则创建文件
fs = new FileStream(filename, FileMode.Create);
}
//数据保存到磁盘中
BinaryWriter bw = new BinaryWriter(fs);
foreach (Student student in stu)
{
bw.Write((String)student.Id);
bw.Write((String)student.Name);
bw.Write((Double)student.Score1);
bw.Write((Double)student.Score2);
bw.Write((Double)student.Score3);
bw.Flush();
}
bw.Close();
fs.Close();
Console.WriteLine("保存成功!");
}
/// 读取信息方法
/// </summary>
/// <returns>读取是否成功</returns>
public void ReadInfo(List<Student> stu)
{
Console.WriteLine("请输入文件读取路径:(键入回车为默认路径)");
String filename = Console.ReadLine();
FileStream fs;
//默认路径
if (filename == "")
{
fs = new FileStream("student.dll", FileMode.Open);
}
else
{
//如果文件不存在,就提示错误
if (!File.Exists(filename))
{
Console.WriteLine("\n\t读取失败!\n错误原因:可能不存在此文件");
return;
}
//否则创建文件
fs = new FileStream(filename, FileMode.Open);
}
//使用二进制读取
BinaryReader br = new BinaryReader(fs);
Console.Write("读取信息将覆盖现有的信息,继续吗?y/n :");
String command = Console.ReadLine();
if (command == "y" || command == "Y")
{
for (int i = 0; i < stu.Count; i++)
{
stu.RemoveAt(i);
}
//从磁盘上读取信息
try
{
while (true)
{
Student student = new Student();
student.Id = br.ReadString();
student.Name = br.ReadString();
student.Score1 = br.ReadDouble();
student.Score2 = br.ReadDouble();
student.Score3 = br.ReadDouble();
stu.Add(student);
student = null;
}
}
catch (Exception)
{
Console.WriteLine("\n\n读取结束!");
}
}
br.Close();
fs.Close();
}
下面是我的写入方法,写入参数List<Student> stu中的数据
/// <summary>
/// 写入信息方法
/// </summary>
/// <returns>写入是否成功</returns>
public void WriteInfo(List<Student> stu)
{
Console.WriteLine("请输入文件保存路径:(键入回车为默认路径)");
FileStream fs;
String filename = Console.ReadLine();
//默认路径
if (filename == "")
{
fs = new FileStream("student.dll", FileMode.Create);
}
//手动输入路径
else
{
//如果文件存在,就提示错误
if (File.Exists(filename))
{
Console.WriteLine("\n\t保存失败!\n错误原因:可能存在相同文件");
return;
}
//否则创建文件
fs = new FileStream(filename, FileMode.Create);
}
//数据保存到磁盘中
BinaryWriter bw = new BinaryWriter(fs);
foreach (Student student in stu)
{
bw.Write((String)student.Id);
bw.Write((String)student.Name);
bw.Write((Double)student.Score1);
bw.Write((Double)student.Score2);
bw.Write((Double)student.Score3);
bw.Flush();
}
bw.Close();
fs.Close();
Console.WriteLine("保存成功!");
}
相关阅读 更多 +