using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; public class LessonFile : MonoBehaviour { // Use this for initialization void Start () { //判斷文件是否存在 if (File.Exists(@"D:\桌面\Directory\Text.txt")) { Debug.Log("文件存在"); } //刪除文件,注意文件的後綴 File.Delete(@"D:\桌面\Directory\Text.txt"); //移動文件,能夠經過這種方式來對文件進行重命名,注意後綴名 //文件名能夠不一樣,後綴名也能夠不一樣(改變文件的類型) File.Move(@"D:\桌面\Directory\JPG.jpg", @"D:\桌面\Directory\PNG.png"); //拷貝文件,先後名字能夠不一致,後綴名也能夠不一致 File.Copy(@"D:\桌面\Directory\PNG.png", @"D:\桌面\Directory\GIF.gif"); //建立文件 FileStream fsCreate = File.Create(@"D:\桌面\Directory\Create.txt"); fsCreate.Close();//關閉文件流 } // Update is called once per frame void Update () { } }
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; public class LessonDirectory : MonoBehaviour { // Use this for initialization void Start () { //獲取當前應用程序(工程)的絕對路徑 Directory.GetCurrentDirectory(); Debug.Log(Directory.GetCurrentDirectory()); //@取消轉義 string str = @"\"; Debug.Log(str); //判斷指定的文件夾路徑是否存在 if (Directory.Exists(@"D:\桌面\Directory")) { Debug.Log("Directory文件夾存在"); } //建立文件夾 Directory.CreateDirectory(@"D:\桌面\Directory\File1"); Directory.CreateDirectory(@"D:\桌面\Directory\File2"); //刪除文件夾,對於文件夾裏有文件的狀況,若是使用一個參數去刪除會報錯 //對於文件夾裏有文件的狀況,咱們使用重載的第二個參數傳入true表示強制刪除 Directory.Delete(@"D:\桌面\Directory\File1"); //移動文件夾,能夠經過這種方式來實現文件夾的重命名 Directory.Move(@"D:\桌面\Directory\File2", @"D:\桌面\Directory\File3"); //獲取文件夾下的全部文件 string[] files = Directory.GetFiles(@"D:\桌面\Directory"); foreach (var item in files) { Debug.Log(item); } //獲取指定路徑下的全部文件夾 string[] directorys = Directory.GetDirectories(@"D:\桌面"); foreach (var item in directorys) { Debug.Log(item); } } // Update is called once per frame void Update () { } }
打開一個文件流以後,必定要關閉,不然會佔用web
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; public class LessonFile : MonoBehaviour { // Use this for initialization void Start () { //判斷文件是否存在 if (File.Exists(@"D:\桌面\Directory\Text.txt")) { Debug.Log("文件存在"); } //刪除文件,注意文件的後綴 File.Delete(@"D:\桌面\Directory\Text.txt"); //移動文件,能夠經過這種方式來對文件進行重命名,注意後綴名 //文件名能夠不一樣,後綴名也能夠不一樣(改變文件的類型) File.Move(@"D:\桌面\Directory\JPG.jpg", @"D:\桌面\Directory\PNG.png"); //拷貝文件,先後名字能夠不一致,後綴名也能夠不一致 File.Copy(@"D:\桌面\Directory\PNG.png", @"D:\桌面\Directory\GIF.gif"); //建立文件 FileStream fsCreate = File.Create(@"D:\桌面\Directory\Create.txt"); fsCreate.Close();//關閉文件流 } // Update is called once per frame void Update () { } }
using System.Collections; using System.Collections.Generic; using UnityEngine; public class LessonTryCatch : MonoBehaviour { public GameObject obj; // Use this for initialization void Start () { Debug.Log("開始try"); try { Debug.Log("try 語句塊!"); Debug.Log(obj.name);//這句話必定會報錯,跳轉catch Debug.Log("打印名字"); } catch (System.Exception e) { Debug.Log("catch 語句塊!" + e); Debug.Log("錯誤信息:" + e); Debug.Log(obj.name);//若是catch報錯,以後的代碼都不會執行 } Debug.Log("結束try"); } // Update is called once per frame void Update () { } }
做業:讀取文本內容,顯示在UI的text組件上,並實現內容滾動數組
文本位置調整網絡
文本自動擴容多線程
讀取文件ide
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; using System.Text; using UnityEngine.UI; public class LessonHomework : MonoBehaviour { public const string filePath = @"D:\桌面\1.txt"; private Text text; private void Awake() { text = transform.Find("Mask/Text").GetComponent<Text>(); } // Use this for initialization void Start () { FileStream fs = File.Open(filePath, FileMode.Open); try { byte[] bytes = new byte[fs.Length]; int ret = fs.Read(bytes, 0, (int)fs.Length); string str = Encoding.UTF8.GetString(bytes); text.text = str; } catch (System.Exception e) { Debug.Log(e.ToString()); } fs.Close(); } // Update is called once per frame void Update () { } }
寫入文件工具
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; using System.Text; public class LessonWriteFile : MonoBehaviour { public const string filePath = @"D:\桌面\Directory\ReadFile.txt"; // Use this for initialization void Start () { //string filePath1 = Directory.GetCurrentDirectory(); //filePath1 = filePath1 + "\\Directory\\"; //寫入文本文件 //一、打開文件 FileStream fs = File.Open(filePath, FileMode.Open); // FileStream fs = File.Open(filePath, FileMode.Append);//追加模式 Debug.Log("遊標的位置:" + fs.Position); //第一個參數:向前移或向後移多少位,第二個參數:從什麼位置開始移動 //對於想要把遊標移動到文件的末尾 //方法1 fs.Seek(fs.Length, SeekOrigin.Begin); //方法2 fs.Seek(fs.Length - fs.Position, SeekOrigin.Current); //方法3 fs.Seek(0, SeekOrigin.End); //對於想要把遊標移動到文件的開始 //方法1 fs.Seek(0, SeekOrigin.Begin); //方法2 fs.Seek(-fs.Position, SeekOrigin.Current); //方法3 fs.Seek(-fs.Length, SeekOrigin.End); Debug.Log("遊標的位置:" + fs.Position); //爲了保證文件打開以後必定能關閉,文件操做寫在Try Catch裏 try { //二、把要寫入的內容轉成字節數組 string str = "作一隻快樂的小魚苗~"; byte[] bytes = Encoding.UTF8.GetBytes(str); //三、寫入文件 //第一個參數:寫入文件的內容轉換成的字符串 //第二個參數:從字節數組中的第幾位開始寫入 //第三個參數:從字節數組中寫入多少位 fs.Write(bytes, 0, bytes.Length); } catch (System.Exception e) { Debug.Log(e.ToString()); } //最終關閉文件流 fs.Close(); } // Update is called once per frame void Update () { } }
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; public class LessonStreamReader : MonoBehaviour { public const string filePath = @"D:\桌面\Directory\ReadFile.txt"; // Use this for initialization void Start () { //文本讀寫器 //打開文件,保證路徑是正確的,有效的 StreamReader sr = new StreamReader(filePath, System.Text.Encoding.UTF8); try { //string str = sr.ReadLine();//只讀取一行。 string str = sr.ReadToEnd();//從開始讀到結尾 Debug.Log("讀取的內容: " + str); } catch (System.Exception e) { Debug.Log(e.ToString()); } //最後必定要關閉文件 sr.Close(); } // Update is called once per frame void Update () { } }
文本寫,不能操做遊標,若是不使用追加模式,默認清空原有內容,從新寫入this
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; public class LessonStreamWrite : MonoBehaviour { public const string filePath = @"D:\桌面\Directory\ReadFile.txt"; // Use this for initialization void Start () { //文本讀寫器 //一、打開文件 //這個重載,會把文件內容所有清除,從新寫入內容 //StreamWriter sw = new StreamWriter(filePath); //這個重載,是追加模式,第二個參數指定是否追加,第三個參數是指定編碼格式 StreamWriter sw = new StreamWriter(filePath, true, System.Text.Encoding.UTF8); try { sw.Write("Hello World!"); } catch (System.Exception e) { Debug.Log(e.ToString()); } //最後必定關閉文件 sw.Close(); } // Update is called once per frame void Update () { } }
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; using System.Threading; public class LessonCopyFile : MonoBehaviour { public UnityEngine.UI.Slider slider; public const string sourceFilePath = @"D:\桌面\1.rar"; public const string targetFilePath = @"D:\桌面\2.rar"; private float maxValue = 1f; private float currentValue; private bool isStop = false; // Use this for initialization void Start () { //小文件的拷貝 //CopyFile(sourceFilePath, targetFilePath); //大文件的拷貝 //TestCopyFile(); //大文件的線程拷貝 //1.申請一個Thread的對象 Thread th = new Thread(TestCopyFile); //2.啓動這個線程 th.Start(); } // Update is called once per frame void Update () { //滑動條賦值 slider.normalizedValue = currentValue / maxValue; } private void OnDestroy() { isStop = true; } //小文件的拷貝 void CopyFile(string sourceFile, string targetFile) { //拷貝,就把源文件的內容寫入到新的文件中 //1.讀取源文件的內容 FileStream source = File.Open(sourceFile, FileMode.Open); byte[] bytes = new byte[source.Length]; try { //讀取內容 source.Read(bytes, 0, bytes.Length); } catch (System.Exception e) { Debug.Log(e.ToString()); } source.Close(); //2.寫入新文件 FileStream target = File.Open(targetFile, FileMode.Create); try { //寫入新文件 target.Write(bytes, 0, bytes.Length); } catch (System.Exception e) { Debug.Log(e.ToString()); } target.Close(); } void TestCopyFile() { Debug.Log("開始拷貝"); CopyBigFile(sourceFilePath, targetFilePath); Debug.Log("拷貝結束"); } void CopyBigFile(string sourceFile, string targetFile) { FileStream source = new FileStream(sourceFile, FileMode.Open); FileStream target = new FileStream(targetFile, FileMode.Create); byte[] bytes = new byte[100];//限制每次拷貝100 * 1024個字節大小的內容 maxValue = source.Length; try { long readCount = 0; //必須保證全部的內容都寫入到另外一個文件中了, 跳出循環 //只要保證每次讀取的長度的累加值 大於等於 文件流的大小 while (readCount < source.Length) { int length = source.Read(bytes, 0, bytes.Length); target.Write(bytes, 0, length); readCount += length; currentValue = readCount; //當組件銷燬時,中止多線程裏的while循環,防止程序中止運行時,多線程仍在執行 if (isStop) { break; } } } catch (System.Exception e) { Debug.Log(e.ToString()); } source.Close(); target.Close(); } }
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; using UnityEngine.UI; public class LessonLoadTexture : MonoBehaviour { public RawImage image; // Use this for initialization void Start () { image.texture = LoadFileTexture(@"D:\桌面\1.jpg"); image.SetNativeSize(); //加載網上的 //StartCoroutine(LoadTexture(@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1527592090920&di=60ad08a7d15d3f91e7a370a9479b5d50&imgtype=0&src=http%3A%2F%2Fimage.yy.com%2Fyywebalbumbs2bucket%2Ffa63ba48c11f46278f9f2be98a141138_1499771837216.jpeg")); //加載本地的 若是要www加載本地的圖片,那麼在路徑前要加 file:// //StartCoroutine(LoadTexture(@"file://D:\桌面\1.jpg")); } // Update is called once per frame void Update () { } Texture LoadFileTexture(string filePath) { //把圖片轉換成字節數組 FileStream fs = File.Open(filePath, FileMode.Open); byte[] bytes = new byte[fs.Length]; try { fs.Read(bytes, 0, bytes.Length); } catch (System.Exception e) { Debug.Log(e.ToString()); } fs.Close(); //把字節數組轉換成圖片 Texture2D tex = new Texture2D(0, 0); if (tex.LoadImage(bytes))//把從字節數組中加載圖片 { return tex; } else { return null; } } IEnumerator LoadTexture(string path) { WWW www = new WWW(path); yield return www;//圖片加載完成後,在執行後面的步驟 //string.IsNullOrEmpty(error) 判斷error不能爲null 也不能爲「」 if (www != null && string.IsNullOrEmpty(www.error)) { image.texture = www.texture; image.SetNativeSize(); } else { Debug.LogError("加載錯誤"); } } }