using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Text;
public static class FileUtility
{
public static string OpenTextFile(string fileName)
{
StreamReader streamReader = File.OpenText(fileName);
if (streamReader == null)
{
Debug.Log("open file failed! filename : " + fileName);
return string.Empty;
}
string fileBuffer = streamReader.ReadToEnd();
streamReader.Close();
streamReader.Dispose();
return fileBuffer;
}
public static string OpenTextFile1(string fileName)
{
FileStream fileStream = new FileStream(fileName,FileMode.Open,FileAccess.Read);
byte[] data = new byte[fileStream.Length];
fileStream.Read(data,0,fileName.Length);
fileStream.Close();
fileStream.Dispose();
return Encoding.UTF8.GetString(data, 0, fileName.Length);
}
public static void writeTxtFile(string fileName, string content, bool appendData = false)
{
CreateDir(GetFilePath(fileName));
byte[] bytes = Encoding.UTF8.GetBytes(content);
FileStream file = null;
if (appendData)
{
file = new FileStream(fileName, FileMode.Append, FileAccess.Write);
}
else
{
file = new FileStream(fileName, FileMode.Create, FileAccess.Write);
}
file.Write(bytes, 0, bytes.Length);
file.Close();
file.Dispose();
}
public static void WriteTextFile1(string fileName,string content, bool appendData = false)
{
FileStream file = null;
if (appendData)
{
file = new FileStream(fileName, FileMode.Append, FileAccess.Write);
}
else
{
file = new FileStream(fileName, FileMode.Create, FileAccess.Write);
}
StreamWriter sw = new StreamWriter(file);
sw.Write(content);
sw.Close();
file.Close();
}
public static bool IsExistFile(string filepath)
{
return File.Exists(filepath);
}
public static void IsExisFolder(string Folderpath)
{
if (!Directory.Exists(Folderpath))
Directory.CreateDirectory(Folderpath);
}
public static void DeleteFile(string filepath)
{
File.Delete(filepath);
}
public static Sprite LoadByIO(string path, int width, int height)
{
FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
fileStream.Seek(0, SeekOrigin.Begin);
byte[] bytes = new byte[fileStream.Length];
fileStream.Read(bytes, 0, (int)fileStream.Length);
fileStream.Close();
fileStream.Dispose();
fileStream = null;
Texture2D texture2D = new Texture2D(width, height);
texture2D.LoadImage(bytes);
return Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), Vector2.zero);
}
public static Texture LoadByIO(string path)
{
FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
fileStream.Seek(0, SeekOrigin.Begin);
byte[] bytes = new byte[fileStream.Length];
fileStream.Read(bytes, 0, (int)fileStream.Length);
fileStream.Close();
fileStream.Dispose();
fileStream = null;
Texture2D texture2D = new Texture2D(0, 0);
texture2D.LoadImage(bytes);
texture2D.Apply();
return texture2D;
}
public static void ValidPath(ref string path)
{
if (path.Length > 0)
{
if (path[path.Length - 1] != '/')
{
path += '/';
}
}
}
public static void DeleteFolder(string path)
{
ValidPath(ref path);
string[] dirlist = Directory.GetDirectories(path);
foreach (var item in dirlist)
{
DeleteFolder(item);
}
string[] fileList = Directory.GetFiles(path);
foreach (var item in fileList)
{
DeleteFile(item);
}
Directory.Delete(path);
}
public static bool DeleteEmptyFolder(string path, bool deleteSelfIfEmpty = true)
{
ValidPath(ref path);
string[] dirList = Directory.GetDirectories(path);
bool isEmpty = true;
foreach (var item in dirList)
{
isEmpty = DeleteEmptyFolder(item, true) && isEmpty;
}
isEmpty = isEmpty && Directory.GetFiles(path).Length == 0;
if (isEmpty && deleteSelfIfEmpty)
{
Directory.Delete(path);
}
return isEmpty;
}
public static void MoveFile(string source, string dest, bool overwrite = true)
{
if (IsExistFile(dest))
{
if (overwrite)
{
DeleteFile(dest);
}
else
{
return;
}
}
else
{
string parentDir = GetFilePath(dest);
CreateDir(parentDir);
}
File.Move(source, dest);
}
public static void CopyFile(string source, string dest, bool overwrite = true)
{
string parentDir = GetFilePath(dest);
CreateDir(parentDir);
File.Copy(source, dest, overwrite);
}
public static int GetFileSize(string file)
{
try
{
FileInfo fileInfo = new FileInfo(file);
int length = (int)fileInfo.Length;
return length;
}
catch
{
return 0;
}
}
public static void CreateDir(string dir)
{
if (IsDirExist(dir))
{
return;
}
string parentDir = GetFilePath(dir);
if (parentDir != dir)
{
CreateDir(parentDir);
}
Directory.CreateDirectory(dir);
}
public static bool IsDirExist(string dir)
{
return Directory.Exists(dir);
}
public static string GetFilePath(string fileName)
{
RightToLeft(ref fileName);
int lastPos = fileName.LastIndexOf('/');
if (lastPos != -1)
{
return fileName.Substring(0, lastPos);
}
return string.Empty;
}
public static void RightToLeft(ref string str)
{
str = str.Replace('\\', '/');
}
public static void LeftToRight(ref string str)
{
str = str.Replace('/', '\\');
}
public static int GetFileCount(string path)
{
DirectoryInfo folder = new DirectoryInfo(path);
FileInfo[] fileInfoList = folder.GetFiles();
int fileCount = fileInfoList.Length;
return fileCount;
}
public static void FindFiles(string path, ref List<string> fileList, List<string> patterns = null, bool recursive = true)
{
ValidPath(ref path);
if (!IsDirExist(path))
{
return;
}
DirectoryInfo folder = new DirectoryInfo(path);
FileInfo[] fileInfoList = folder.GetFiles();
int fileCount = fileInfoList.Length;
int patternCount = patterns != null ? patterns.Count : 0;
for (int i = 0; i < fileCount; ++i)
{
string fileName = fileInfoList[i].Name;
if (patternCount > 0)
{
for (int j = 0; j < patternCount; ++j)
{
if (EndWith(fileName, patterns[j], false))
{
fileList.Add(path + fileName);
}
}
}
else
{
fileList.Add(path + fileName);
}
}
if (recursive)
{
string[] dirs = Directory.GetDirectories(path);
foreach (var item in dirs)
{
findFiles(item, ref fileList, patterns, recursive);
}
}
}
public static bool EndWith(string oriString, string pattern, bool sensitive = true)
{
if (oriString.Length < pattern.Length)
{
return false;
}
string endString = oriString.Substring(oriString.Length - pattern.Length, pattern.Length);
if (sensitive)
{
return endString == pattern;
}
else
{
return endString.ToLower() == pattern.ToLower();
}
}
}