注意需要引用 using System.IO;
1.用File读取后得到txt文本内容,需要引用IO接口。
string STR= File.ReadAllText(URL);//File.ReadAllText(读取地址Path)"URL"为读取txt文本地址例如:Application.streamingAssetsPath+ "/Test.txt"
2.利用File 写入修改后的文件 (STR修改后的数据)。
File.WriteAllText(URL, STR);//File.WriteAllText(“URL”为写入Txt文本的地址如:Application.streamingAssetsPath + "/Test.txt",“STR” 为更改后的txt文本内容,该内容为String类型)
3.使用File.Exists判断路径是否存在。
string Path = Application.streamingAssetsPath + "/txt.txt";;
if (!File.Exists(Path))/判断URL本地 地址是否存在,如果不存在则不向下执行。存在则继续执行
{
print("地址存在");
}
else
{
print("地址不存在");
}
4.File.Create创建文本。
String Path;
Path = Application.streamingAssetsPath + "/txt.txt";
File.Create(Path).Close();
5.读取方法简单使用。
private string Path;
void ReadTxt()
{
Path = Application.streamingAssetsPath + "/txt.txt";
if (!File.Exists(Path))/判断URL本地 地址是否存在,如果不存在则不向下执行。存在则继续执行
{
Debug.Log(Path + "地址不存在");
return;
}
else
{
Debug.Log("没有找到txt文本");
File.Create(Path).Close();//创建文本
//File.WriteAllText(Path,"0");//向文本中写入内容
}
File.ReadAllText(URL);//“URL”为读取文件的地址信息
}
4.写入方法简单使用。
private string Path;
void ReadTxt()
{
Path = Application.streamingAssetsPath + "/txt.txt";
if (!File.Exists(Path))//判断Path本地 地址是否存在,如果不存在则不向下执行。存在则继续执行
{
Debug.Log(Path+ "地址不存在");
return;
}
File.WriteAllText(Path,string);//“Path”为写入文件的地址信息,注意改地址与读取地址一致否则可能读取失败
}