public Text readText;
private string path;
private string path2;
private string content;
private string[] contents;
// Use this for initialization
void Start () {
path = Application.dataPath + "\\test.txt";
path2 = Application.dataPath + "\\test1.txt";
content = "窗前明月光|疑是地上霜|举头望明月|低头思故乡";
contents = content.Split('|');
}
// Update is called once per frame
void Update () {
}
//写入数据
public void WriteTxt()
{
if(!File.Exists(path))
{
//文本不存在创建文本
FileStream fileStream = new FileStream(path, FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fileStream, Encoding.UTF8);
foreach (var item in contents)
{
sw.WriteLine(item);
}
sw.Close();
fileStream.Close();
}
else
{
}
}
//读取数据
public void ReadTxt()
{
if(File.Exists(path))
{
StreamReader sr = new StreamReader(path);
readText.text = sr.ReadToEnd();
sr.Close();
}
}
//删除文本
public void DeleTxt()
{
FileInfo fileInfo = new FileInfo(path);
fileInfo.Delete();
}
//拷贝文本
public void CopyTxt()
{
if(File.Exists(path)&&File.Exists(path2))
{
string content;
StreamReader sr = new StreamReader(path);
content= sr.ReadToEnd();
StreamWriter sw = new StreamWriter(path2);
sw.Write(content);
sr.Close();
sw.Close();
}
}
}