读取文件
读分为字符数据读取(StreamReader)和字节数据读取(FileStream)
StreamReader与FileStream区别
- FileStream可读可写,而StreamReader只能读不能写
2.StreamReader关闭后,与之相关的FileStream没有关闭,(通过FileStream的CanRead的测试)
等。。。
参考:https://www.iteye.com/blog/zhuogx-1535691
写入文件
当前文件写入非字符类型数据时,StreamWriter与BinaryWriter 存在巨大的差异。
StreamWriter 是把各种类型的数据都转化成字符,然后把字符按照一定的格式编码出来的数据写入文件
BinaryWriter 是直接把数据在内存中的真实内容写入到指定文件中
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
namespace ZYL_File
{
public class File_IO : MonoBehaviour
{
private float time;
private bool state = false;
string path_Start = @"D:/File_Start/Content0723.rar";
string path_End = @"D:/File_Start/www.rar";
private int byteSize = 1024 * 1024 * 5;
void Start()
{
SegRead(path_Start, path_End);
}
// Update is called once per frame
void Update()
{
}
/// <summary>
/// 超2GB文件的读取与写入
/// </summary>
/// <param name="filepath"></param>
/// <param name="outpath"></param>
public async void SegRead(string filepath, string outpath)
{
if (File.Exists(outpath))
{
File.Delete(outpath);
}
File.Create(outpath).Dispose();
FileStream fsread = new FileStream(filepath, FileMode.Open, FileAccess.Read);
FileStream fswrite = File.OpenWrite(outpath);
BinaryReader r = new BinaryReader(fsread);
byte[] bs = r.ReadBytes(byteSize);
while (bs.Length != 0)
{
await Task.Run(() => {
fswrite.Write(bs, 0, bs.Length);
fswrite.Flush();
Debug.Log("write.......");
//从当前流中读取指定的字节数以写入字节数组中,并将当前位置前移相应的字节数。
bs = r.ReadBytes(byteSize);
Debug.Log("进度:" + fswrite.Length * 1f / fsread.Length);
});
}
fsread.Close();
fswrite.Close();
fsread.Dispose();
fswrite.Dispose();
Debug.Log("write finish");
}
/*
FileStream 类操作的是字节和字节数组,而 StreamReader 或 StreamWriter 操作的是字符数据。
*/
//-----------------------------------
public void UseTask()
{
Task.Run(() =>
{
Test(path_Start, path_End);
state = true;
Debug.Log(time);
});
}
// Thread.Sleep(1000); //延迟执行一秒
/// <summary>
/// 读取和写入文件
/// </summary>
/// <param name="path_Start"></param>
/// <param name="path_End"></param>
private void Test(string path_Start, string path_End)
{
if (File.Exists(path_End))
{
File.Delete(path_End);
}
File.Create(path_End).Dispose();
WriteFileData(ReadFileByte(path_Start), path_End);
}
/// <summary>
/// 读取文件
/// </summary>
private byte[] ReadFileByte(string path_Start)
{
FileStream fileStream = new FileStream(path_Start, FileMode.Open, FileAccess.Read); //创建读取流
fileStream.Seek(0, SeekOrigin.Begin);
byte[] buff = new byte[fileStream.Length]; //创建文件长度缓冲区
//释放文件读取流
fileStream.Close();
fileStream.Dispose();
return buff;
}
/// <summary>
/// 写入文件(字节)
/// </summary>
private void WriteFileData(byte[] buff, string path)
{
FileStream fs_Write = new FileStream(path, FileMode.Open, FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs_Write);
bw.Write(buff, 0, buff.Length);
bw.Close();
bw.Dispose();
}
//-----------------------------------------
/// <summary>
/// 读取全部的内容(字符数据)
/// </summary>
private void ReadFile()
{
string path_Start = @"D:/File_Start/Test.txt";
StreamReader sr2 = new StreamReader(path_Start);
string nextLine;
while ((nextLine = sr2.ReadLine()) != null)
{
Debug.Log(nextLine + "??");
}
sr2.Close();
sr2.Dispose();
}
//------------------------------------------
/// <summary>
/// 例子一 向文件写入数据
/// </summary>
private void SaveFile_Simlpe()
{
string path = @"D:/File_Start/Test.txt";
StreamWriter sw1;
if (!File.Exists(path))
{
sw1 = File.CreateText(path);
}
else
{
sw1 = new StreamWriter(path);
}
for (int i = 0; i < 100; i++)
{
string str = "测试" + i.ToString() + "\n";
sw1.Write(str);
}
sw1.Close();
sw1.Dispose();
}
}
}
···
网友评论