第十二章文件处理
目标
- 了解System.IO命名空间
- 文件和目录的常用操作方法
- 使用文件流读写文本文件
- 使用文件流读写二进制文件
- 掌握打开文件、保存文件对话框的使用
- 序列化和反序列化
System.IO命名空间
- 对文件操作
System.lo命名空间
- 操作各种流类数据
System.Text命名空间
- 关于字符编码的类
File类
- 文件操作静态类
方法 | 描述 |
---|---|
Create(string filePath) | 在指定路径下创建指定名称的文件 |
Copy(string sourceFile,string desFile) | 按指定路径将源文件中的内容复制到目标文件中,如果目标文件不存在将新建目标文件 |
Delete(string filePath) | 删除指定路径的文件 |
Exists(string filePath) | 验证指定路径的文件是否存在 |
Move(string sourceFile,string desFile) | 将指定文件移动到一个新的路径 |
实例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;//读写文件流操作
namespace Lesson12_1
{
class Program
{
static void Main(string[] args)
{
//File类内包含用于处理文件的静态方法
if (!File.Exists(@"D:\aa.txt"))
{
File.Create(@"D:\aa.txt");
}
else
{
File.Copy(@"D:\aa.txt", @"D:\bb.txt");
}
File.Delete(@"D:\aa.txt");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Lesson12_2
{
class Program
{
static void Main(string[] args)
{
//Directory类包含了处理目录的静态方法
Directory.CreateDirectory("@D:/测试目录");
}
}
}
Directory类
- 目录(文件夹)操作静态类
方法 | 描述 |
---|---|
CreateDirectory(string path) | 创建目录 |
Dele(string path[,bool recursive]) | 删除指定的目录,如果第二个参数为true则同时删除该目录下的所有文件和子目录 |
Exists(string path) | 测试目录是否存在 |
GetCurrentDirectory() | 获得应用程序的当前工作目录 |
GetDirectories(string path) | 返回代表子目录的字符串数组 |
GetFiles(string path) | 以字符串数组形式返回指定目录中的文件的名称 |
Move(string sourcePath,string desPath) | 将目录及其内容移到指定的新位置 |
读写文本文件
文件流
FileStream
- 构造方法FileStream(filePath,FileMode,[FileAccess],[FileShare])
- filePath是string类型
- 其余3个是枚举类
流读取对象
StreamWriter类流写入对象
- Write()
- WriteLine()
- Close()
StreamReader类流读取对象
- ReadLine()
- ReadToEnd()
- Close()方法
二进制文件的读写
- BinaryReader类
- BinaryWriter类
实例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Lesson12_3
{
class Program
{
static void Main(string[] args)
{
//1、
FileStream fs = new FileStream(@"D:\二进制.data",FileMode.Create);
//2、二进制写入对象
BinaryWriter bw = new BinaryWriter(fs);
//3、写入二进制文件
for (int i = 0; i < 10; i++)
{
bw.Write(i);
}
Console.WriteLine("已写入");
//4、
bw.Close();
//5、
fs.Close();
Console.WriteLine("=================================读取二进制文件=============================================");
//1、
FileStream fr = new FileStream(@"D:\二进制.data", FileMode.Open,FileAccess.Read);
//2、二进制读取对象
BinaryReader br = new BinaryReader(fr);
//3、读取二进制文件
try
{
while (true)
{
int num = br.ReadInt32();
Console.WriteLine(num);
}
}
catch (EndOfStreamException ex)
{
Console.WriteLine("读取完成");
}
//4、
bw.Close();
//5、
fs.Close();
}
}
}
复制图片到指定目录
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Lesson12_4
{
class Program
{
static void Main(string[] args)
{
//读取图片的二进制信息
FileStream fs = new FileStream(@"D:\Desktop\示例图片.jpg", FileMode.Open,FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
//读取文件信息
byte[] b = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
//复制图片--其实就是在另一个路径写入图片信息
FileStream fsWrite = new FileStream(@"E:\示例图片1.jpg", FileMode.Create);
BinaryWriter bw = new BinaryWriter(fsWrite);
//将读出的字节数组写入到文件
bw.Write(b);
Console.WriteLine("图片复制成功");
bw.Close();
fsWrite.Close();
}
}
}
序列化和反序列化
序列化
- 将 对象 的状态存储到特定的文件中
- Serialize()
反序列化
- 是序列化的逆向过程,也就是将文件里保存的数据还原为对象
- Deserialize()
步骤
- 引用命名空间
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
- [Serializable]必须写在类定义前面,表示后面的类可以被序列化
实例
序列化是面向对象的所以先创建一个类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lesson12_5
{
[Serializable]//[Serializable]必须写在类定义前面,表示后面的类可以被序列化
class Student
{
public int StuId { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Sex { get; set; }
public Student(int StuId, string Name,int Age,string Sex)
{
this.StuId = StuId;
this.Name = Name;
this.Age = Age;
this.Sex = Sex;
}
}
}
然后对实例化这个类并进行序列化和反序列化操作
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
namespace Lesson12_5
{
class Program
{
static void Main(string[] args)
{
Student stu = new Student(1,"fsy",29,"Man");
BinaryFormatter bf = new BinaryFormatter();
FileStream fs = new FileStream(@"D:\MyFile.bin", FileMode.Create, FileAccess.Write);
//对象序列化
bf.Serialize(fs,stu);
fs.Close();
Console.WriteLine("序列化成功");
Console.WriteLine("=============反序列化============");
//BinaryFormatter bf = new BinaryFormatter();
FileStream fsReader = new FileStream(@"D:\MyFile.bin", FileMode.Open, FileAccess.Read);
//反序列化
Student stu1= bf.Deserialize(fsReader) as Student;
fsReader.Close();
Console.WriteLine("学号{0},姓名{1},年龄{2},性别{3}",stu1.StuId,stu1.Name,stu1.Age,stu1.Sex);
}
}
}
总结
File类
- 静态对象
- 实现对文件的创建、拷贝、移动和删除等操作
Directory类
- 静态对象
- 实现对目录(文件夹)的操作
- 文件流对象
FileStream对象
- 创建该对象时需要指定操作文件的路径、文件的打开方式
- 文件读写权和文件访问方式(可选)
流读写对象
- StreamReader是流读取对
- StreamWriter是文件写入对象。
二进制文件的读写
- BinaryReader
- BinaryWriter
序列化和反序列化
- 序列化是将对象的状态存储到特定的文件中。
- 反序列化是将存储在文件中的数据重新构建为对象
- 类对象是否可被序列化,关键是在类的头部添加[Serializable]关键字
评论 (0)