1、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();//將文件刪除 }
2、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("張婷是壞人");//在最底層文件夾裏建立一個名爲「張婷是壞人」的子文件夾
3、File 文件讀取和寫入操做:rest
一、以行的形式讀取文件,一行一個字符串,返回一個字符串的數組。寫入文件 (寫入的時候建立文件,並向裏面寫入,若是文件名字相同則覆蓋) 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); 二、根據文件路徑讀取文件中全部的文本和寫入文件 (寫入的時候建立文件,並向裏面寫入,若是文件名字相同則覆蓋) string str = File.ReadAllText(@"F:\張曉坤\01\FileReadPath.txt"); Debug.Log(str); File.WriteAllText(@"F:\張曉坤\01\FileWritePathTwo.txt",str); 三、方法能夠打開二進制文件把內容讀入一個byte數組 byte[] bytes = File.ReadAllBytes(@"F:\張曉坤\01\01.mp4"); File.WriteAllBytes(@"F:\張曉坤\01\02.mp4", bytes);
4、使用FileStream讀取和寫入文件:code
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();//關閉寫入流 } } }
5、使用StreamReader 和 StreamWriter讀取和寫入文件:string
一)、it
咱們對文本文件的讀寫通常使用StreamReader和StreamWriter,由於不一樣的文本有不一樣的編碼格式,這個StreamReader會幫咱們自動處理,因此咱們不須要關心文本文件的編碼是什麼io
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寫入長度