您的位置:首页 > 其它

文件的读写操作

2017-09-04 00:00 218 查看
一、FileInfo 文件操作:

//将 FileInfo 类用于典型的文件操作,
//如复制、移动、重命名、创建、打开、删除和追加到文件。
FileInfo fileInfo = new FileInfo(path);
if (!fileInfo.Exists)
{
fileInfo.Create();//如果fileInfo文件不存在,则根据上面的路径创建文件
Debug.Log("创建文件....");
}
else
{
Debug.Log(fileInfo.Name);//文件的名字包括:文件名 + 后缀
Debug.Log(fileInfo.Exists);//检查文件是否存在
Debug.Log(fileInfo.Directory);//取得文件所在路径
Debug.Log(fileInfo.Length);//获取文件的大小,单位是字节
Debug.Log(fileInfo.IsReadOnly);//文件是否是只读的
fileInfo.CopyTo("D:/11/copyFile.txt");//把fileInfo文件拷贝到D:/11/路径下,名字是copyFile.txt的文件

fileInfo.MoveTo("D:/11/copyFile2.txt");//根据指定路径和文件名移动文件
fileInfo.Delete();//将文件删除
}

二、DirectoryInfo 文件夹操作:

public string DirPath;//这是一个文件夹路径
//DirectoryInfo这个类是对文件夹进行操作,FileInfo是对文件进行操作.要进行区别
DirectoryInfo dirInfo = new DirectoryInfo(DirPath);

if (dirInfo.Exists)
{
Debug.Log("Create......");
dirInfo.Create();//如果文件夹不存在,则创建一个新的文件夹
}

Debug.Log(dirInfo.Exists);//判断文件夹是否存在
Debug.Log(dirInfo.Name);//获取最底层文件夹的名字
Debug.Log(dirInfo.Parent);//获取最底层的上一层文件夹的名字
Debug.Log(dirInfo.Root);//获取最上层文件夹的名字
Debug.Log(dirInfo.CreationTime);//最底层文件夹的创建时间

dirInfo.CreateSubdirectory("张婷是坏人");//在最底层文件夹里创建一个名为“张婷是坏人”的子文件夹


三、File 文件读取和写入操作:

1、以行的形式读取文件,一行一个字符串,返回一个字符串的数组。写入文件
(写入的时候创建文件,并向里面写入,如果文件名字相同则覆盖)
string[] strArray = File.ReadAllLines(@"F:\张晓坤\01\FileReadPath.txt");//逐行读取文件,
for (int i = 0; i < strArray.Length; i++)
{
Debug.Log(strArray[i]);
}
File.WriteAllLines(@"F:\张晓坤\01\FileWritePathOne.txt", strArray);

2、根据文件路径读取文件中所有的文本和写入文件
(写入的时候创建文件,并向里面写入,如果文件名字相同则覆盖)
string str = File.ReadAllText(@"F:\张晓坤\01\FileReadPath.txt");
Debug.Log(str);
File.WriteAllText(@"F:\张晓坤\01\FileWritePathTwo.txt",str);

3、方法可以打开二进制文件把内容读入一个byte数组
byte[] bytes = File.ReadAllBytes(@"F:\张晓坤\01\01.mp4");

File.WriteAllBytes(@"F:\张晓坤\01\02.mp4", bytes);


四、使用FileStream读取和写入文件:

FileStream适合读取二进制流文件,而不适合用于txt文件的读写.

案例:copy一个日本AV视频

代码如下:

using UnityEngine;
using System.Collections;
using System.IO;

public class FileController : MonoBehaviour {

const string DirPath = @"E:\AV\";

void Start () {
copyAV();
}

void copyAV()
{
//创建一个读取流
FileStream readStream = new FileStream(DirPath + "日本AV.mp4", FileMode.Open);
//创建一个写入流
FileStream writeStream = new FileStream(DirPath + "Copy日本AV.mp4", FileMode.Create);
//创建一个byte数组,用于存储读取数据
byte[] data = new byte[1024];
while (true)
{
//length是一次读取数据的长度
//data是存储读取的数据
//0 是偏移0个字节
//data.length是一次读取data.length个字节
int length = readStream.Read(data,0,data.Length);
//如果读取数据的长度“length”是0,说明后面没有数据了,则停止读取break掉
if (length == 0)
{
Debug.Log("读取结束....");
break;
}
else
{
//如果length不为0,则写入数据
writeStream.Write(data,0,data.Length);
}

readStream.Close();//关闭读取流
writeStream.Close();//关闭写入流
}
}
}


五、使用StreamReader 和 StreamWriter读取和写入文件:

一)、

我们对文本文件的读写一般使用StreamReader和StreamWriter,因为不同的文本有不同的编码格式,这个StreamReader会帮我们自动处理,所以我们不需要关心文本文件的编码是什么

1,创建文本的读取流(会检查字节码标记确定编码格式)

StreamReader sr = new StreamReader(@"c:\xx\ReadMe.txt");

2,指定编码格式

StreamReader str = new StreamReader(@"c:\xx\xx.txt",Encoding.UTF8);

(可取的编码格式 ASCII Unicode UTF7 UTF8 UTF32)

3,在文件流的基础上创建文本读取流

FileStream fs = new FileStream(@"c:\xx\xx.txt",FileMode.Open,FileAccess.Read,FileShare.None);

StreamReader sr = new StreamReader(fs);

4,通过文件信息创建文本读取流-第二种方式

FileInfo myFile = new FileInfo(@"c:\xx\xx.txt");

StreamReader sr = myFile.OpenText();

流的关闭 sr.Close();

二)、读取文件

1,string nextLine = sr.ReadLine();//读取一行字符串

2,string restOfStream = sr.ReadToEnd();//读取流中所有剩余的文本内容

3,int nextChar = sr.Read();//只读取一个字符

4, int nChars = 100;

char[] charArray = new char[nChars];

int nCharsRead = sr.Read(charArray,0,nChars); 读取多个字符,第一个参数是要存放的字符数组,第二个参数是从数组的哪一个索引开始放,第三个参数是读取多少个字符 返回值是实际读取的字符的个数

三)、创建写入文件

StreamWriter的创建

1,StreamWriter sw = new StreamWriter(@"c:\xx\xx.txt");(默认使用UTF-8编码)

2,StreamWriter sw = new StreamWriter(@"c:\xx\xx.txt",true,Encoding.ASCII)

第二个参数表示是否以追加的方式打开,第三个参数是编码方式

3,通过FileStream创建StreamWriter

FileStream fs = new FileStream(@"c:\xx\xx.txt",FileMode.CreateNew,FileAccess.Write,FileShare.Read);

StreamWriter sw = new StreamWriter(fs);

4,通过FileInfo创建StreamWriter

FileInfo myFile = new FileInfo(@"c:\xx\xx.txt");

StreamWriter sw = myFile.CreateText();

所有流用完之后关闭 sw.Close();

四)、写入文件

1,写入一行字符

string nextLine = "x xx x x x x ";sw.Write(nextLine);

2,写入一个字符

char nextChar = 'a';

sw.Write(nextChar);

3,写入字符数组

char[] charArray = ..;

sw.Write(charArray);

4,写入字符数组的一部分

sw.Write(charArray,StartIndex,Length);

1:要写入的数组 2:开始索引 3写入长度
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  文件操作