Skip to content
字数
1168 字
阅读时间
5 分钟

可以通过FileStream一部分一部分地读写数据流 类名:FileStream(System.IO)

打开或创建指定文件

方法一:new FileStream

cs
	FileStream fs = new FileStream(Application.dataPath + "/Lesson3.tang", FileMode.Create, FileAccess.ReadWrite);
	//参数一:路径
	//参数二:打开模式
	//  CreateNew:创建新文件 如果文件存在 则报错
	//  Create:创建文件,如果文件存在 则覆盖
	//  Open:打开文件,如果文件不存在 报错
	//  OpenOrCreate:打开或者创建文件根据实际情况操作
	//  Append:若存在文件,则打开并查找文件尾,或者创建一个新文件
	//  Truncate:打开并清空文件内容
	//参数三:访问模式
	//参数四:共享权限
	//  None 谢绝共享(默认)
	//  Read 允许别的程序读取当前文件
	//  Write 允许别的程序写入该文件
	//  ReadWrite 允许别的程序读写该文件

方法二:File.Create

cs
	FileStream fs2 = File.Create(Application.dataPath + "/Lesson3.tang");
	//参数一:路径
	//参数二:缓存大小(最大限制)
	//参数三:描述如何创建或覆盖该文件(不常用)
	//  Asynchronous 可用于异步读写
	//  DeleteOnClose 不在使用时,自动删除
	//  Encrypted 加密
	//  None 不应用其它选项
	//  RandomAccess 随机访问文件
	//  SequentialScan 从头到尾顺序访问文件
	//  WriteThrough 通过中间缓存直接写入磁盘

方法三:File.Open

cs
FileStream fs3 = File.Open(Application.dataPath + "/Lesson3.tang", FileMode.Open);
	//参数一:路径
	//参数二:打开模式

重要属性和方法

cs
	FileStream fs = File.Open(Application.dataPath + "/文件名", FileMode.OpenOrCreate);
	//文本字节长度
	print(fs.Length);
	if(fs.CanRead){}!
	if(fs.CanWrite){}

将字节写入文件后 一定执行一次

cs
	fs.Flush();

文件读写完毕后 一定执行

cs
	fs.Close();

缓存资源销毁回收

cs
	fs.Dispose();

写入字节

cs
	FileStream fs = new FileStream(Application.persistentDataPath(可读写路径) + "/文件名", FileMode.OpenOrCreate, FileAccess.Write);
	byte[] bytes = BitConverter.GetBytes(999);
	fs.Write(bytes, 0, 4);
	//fs.Write(bytes, 0, bytes.Length);
	//参数一:写入的字节数组
	//参数二:数组中的开始索引
	//参数三:写入多少个字节
	写入字符串---先写入长度 再写入字符串具体内容
	bytes = Encoding.UTF8.GetBytes("一个字符串");
	fs.Write(BitConverter.GetBytes(bytes.Length), 0, 4); //写长度
	fs.Write(bytes, 0, bytes.Length); //写内容

读取字节

方法一:挨个读取字节数组

cs
	FileStream fs2 = File.Open(Application.persistentDataPath + "/文件名", FileMode.Open, FileAccess.Read);
	byte[] bytes2 = new byte[4];
	int index = fs2.Read(bytes2, 0, 4);
	//参数一:用于存储读取的字节数组的容器
	//参数二:容器中开始的位置
	//参数三:读取多少个字节装入容器
	//返回值:当前流索引前进了几个位置

读取字符串

cs
	index = fs2.Read(bytes2, 0, 4);
	int length = BitConverter.ToInt32(bytes2, 0);
	//要根据我们存储字符串的长度 声明新的字节数组 用来装载读出来的数据
	bytes2 = new byte[length];
	fs2.Read(bytes2, 0, length);
	print(Encoding.UTF8.GetString(bytes2));
	fs2.Dspose();

方法二:一次性读取再挨个读取

cs
	FileStream fs3 = File.Open(Application.persistentDataPath + "/文件名", FileMode.Open, FileAccess.Read);
	byte[] bytes3 = new byte[fs3.Length];
	fs3.Read(bytes3, 0, (int)fs3.Length);
	fs3.Dispose();
	//将字节数组 `bytes3` 的前四个字节(从索引0开始)转换为一个整数,并打印出来
	print(BitConverter.ToInt32(bytes3, 0));
	//将字节数组 `bytes3` 的接下来的四个字节(从索引4开始)转换为一个整数,这个整数表示字符串的长度
	int length2 = BitConverter.ToInt32(bytes3, 4);
	//从字节数组 `bytes3` 中的第8个字节开始,读取长度为 `length2` 的字节,并将其转换为UTF-8编码的字符串,然后打印出来
	print(Encoding.UTF8.GetString(bytes3, 8, length2));

方法三:更加安全的使用文件流对象

*using关键字的重要用法

cs
//using (申明一个引用对象)
//{
//使用对象
//}
//无论发生什么情况 当using语句块结束后
//会自动调用该对象的销毁方法 避免忘记销毁或关闭流
//using是一种更安全的使用方法

using (FileStream fs2 = File.Open(Application.persistentDataPath + "/文件名", FileMode.Open, FileAccess.Read))
{
	byte[] bytes2 = new byte[4];
	int index = fs2.Read(bytes2, 0, 4);
	int i = BitConverter.ToInt32(bytes2, 0);
	print("取出来的第一个整数" + i);//999
	print("索引向前移动" + index + "个位置");
	//读取第二个字符串
	//读取字符串字节数组长度
	index = fs2.Read(bytes2, 0, 4);
	print("索引向前移动" + index + "个位置");
	int length = BitConverter.ToInt32(bytes2, 0);
	//要根据我们存储的字符串字节数组的长度 来声明一个新的字节数组 用来装载读取出来的数据
	bytes2 = new byte[length];
	index = fs2.Read(bytes2, 0, length);
	print("索引向前移动" + index + "个位置");
	//得到最终的字符串 打印出来
	print(Encoding.UTF8.GetString(bytes2));
	fs2.Dispose();
}

贡献者

The avatar of contributor named as worldddddd worldddddd

文件历史

撰写