I/O类
1.1常见的非抽象类
使用这些类需要引用System.IO命名空间
1.2Directory类
Directory和DirectoryInfo的区别:
Directory使用类里的方法时,可以直接调用,DirectoryInfo则需要进行实例化来调用,若对一个对象需要进行多种操作,建议使用DirectoryInfo
包含的常见方法:
CreateDirectory(string path):在指定路径中创建目录,存在则不创建。
Delete(string path):从指定路径中删除目录。
Delete(string path,bool recursive):从指定路径中删除目录,recursive递归决定是否删除子目录。若文件子目录不为空,recursive为false时(不删除),则会报错
Exists(string path):判断文件夹是否存在。
Move(string path,string path):移动文件,第一个参数是源地址,第二个参数是目标地址。若文件目标地址存在,则会报错。
Attributes:设置文件夹的属性。
FileAttributes是枚举类型
ReadOnly:只读
Hidden:隐藏文件夹
Temporary:临时文件夹
Encrypted:加密文件夹
Directory.CreateDirectory(@"C:\Temp");
Directory.Delete(@"C:\Temp");
bool isExists=Directory.Exists(@"C:\Temp");
Directory.Move(@"C:\Temp",@"C:\Temp2");
Directory.CreateDirectory(@"C:\Temp").Attributes=FileAttributes.ReadOnly;
DirectoryInfo info = new DirectoryInfo(@"C:\Temp");
info.Create();
info.Delete();
info.Exists();
info.MoveTo(@"C:\Temp2");
info.Attributes=FileAttributes.ReadOnly;
1.3File类
File与FileInfo的区别和Directory与DirectoryInfo的区别相同。
包含的常见方法:
Create(string path):在指定路径创建或覆盖文件。
Delete(string path):删除指定文件。
Exists(string path):判断文件是否存在。
Copy(string path,string path):复制文件,第一个参数是源地址,第二个参数是目标地址。
Move(string path,string path):移动文件,第一个参数是源地址,第二个参数是目标地址。
Open(string path,filemode mode):打开指定路径的文件,指定读/写访问模式。
filemode枚举类型:
Open:以只读形式打开文件
Create:以写的形式打开
OpenOrCreate:以读/写
CreateNew:以创建新文件的形式打开
ReadAllLines(string path):打开一个文本文件,读取文件的所有行,然后关闭该文件。
ReadAllLines(string path,System.Text.Encoding encoding):打开一个文件,使用指定的编码读取文件的所有行,然后关闭该文件。
File.Copy(@"C:\Temp",@"C:\Temp2");
File.Open(@"C:\Temp",FileMode.OpenOrCreate);
FileInfo info = new FileInfo(@"C:\Temp");
info.MoveTo(@"C:\Temp2");
string[] fsRead = File.ReadAllLines("./1.txt", Encoding.UTF8);//逐行读取文件中的信息到字符串数组中
// "./"表示当前项目相对地址,"1.txt"为访问文件名,Encoding.UTF8位字符格式\
string fstr = File.ReadAllText("./1.txt");//读取文件中的信息到字符串中
string fwrite = "\n32131kujjkjsncjk";
File.WriteAllText("./1.txt",fwrite);//向文件中写入信息(覆盖形式)
File.AppendAllText("./1.txt", "jbkdas很快");//向文件中写入信息(追加的形式)
string[] fw = { "坚持住", "很快就可以吃饭了" };
File.AppendAllLines("./1.txt", fw);//向文件中一行一行写入信息
1.4Stream类
Stream类用于从文件中读取二进制数据,或使用流读/写文件。
Stream类常常要使用File类方法打开文件,然后使用流读/写文件,也有直接的文件流类FileStream类以文件流的形式打开文件。
包含的常见方法:
Read:从基础流中读取数据,并把流的当前位置往前移。
Close:关闭当前Stream对象和基础流。
Write:把数据写入基础流中。
Flush:清理当前所有缓冲区,使所有缓冲数据写入基础设备。
Seek:设置当前流内的位置。
FileStream文件流对象写入和读取数据实例代码:引入System.Text命名空间
if(!File.Exists(path))
{
using (FileStream fs=File.Create(path))//创建文件
{
byte[] info = new UTF8Encoding(true).GetBytes("写入的内容");
fs.Write(info, 0, info.Length);//写入内容
}
}
if(File.Exists(path))//读取数据
{
using (FileStream fs=File.Open(path,File.Open,FileAccess.Read,FileShare.None))//打开文件
{
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);
while(fs.Read(b,0,b.Length)>0)//Read()返回读入缓冲区中的总字节数,此数组的值被从当前文件中读取的字节所替换,第二个参数为数组中的字节偏移量,将在此处放置读取的字节。第三个参数为最多读取的字节数
{
Debug.Log(temp.GetString(b));
}
}
}
1.5StreamReader和StreamWriter
(learning)
JSON
Serializable 序列化的是可序列化的类或结构。
JSON数据实例代码:
void Start()
{
WriteData();
}
public void WriteData()//写数据
{
Data m_Data = new Data();
m_Data.Person = new Person[3];
Person p1 = new Person();
p1.Name = "";
p1.Grade = 97;
m_Data.Person[0] = p1;
Person p2 = new Person();
p2.Name = "";
p2.Grade = 97;
m_Data.Person[1] = p2;
Person p3 = new Person();
p3.Name = "";
p3.Grade = 97;
m_Data.Person[2] = p3;
string js = JsonUtility.ToJson(m_Data);//将数据转成JSON
Debug.Log(js);
string fileUrl = @"C:\Temp\jsonInfo.txt";
StreamWriter sw = new StreamWriter(fileUrl);//创建或打开文档
sw.WriteLine(js);//写入数据
sw.Close();//关闭文档
}
public string ReadData()//读取数据
{
string fileUrl = @"C:\Temp\jsonInfo.txt";
StreamRead str = File.OpenText(fileUrl);//打开文件
string readData = str.ReadToEnd();//读取数据保存到readData中
str.Close();
return readData;//返回数据
}
public void ParseData(string jsonData)//解析json数据
{
Data m_ParseData JsonUtility.FromJson<Data>(jsonData);//将json数据保存到m_ParseData中
foreach(var item in m_ParseData)//可用var代表未知类型
{
Debug.Log(item.Name);
Debug.Log(item.Grade);
}
}
[System.Serializable]
class Person
{
public string Name;
public int Grade;
}
[System.Serializable]
class Data
{
public Person[] Person;
}