使用System.IO命名空间下的类读写文件
void Update () {
if (Input.GetKeyDown(KeyCode.Space))
{
using (StreamWriter st = new StreamWriter(@"E:\abc.txt", true, Encoding.Default))
{
st.Write("写入文件到txt");
st.Close();
}
Debug.Log("写入成功");
}
if (Input.GetKeyDown(KeyCode.A))
{
using (StreamReader sr = new StreamReader(@"E:\abc.txt", Encoding.Default))
{
int i = 1;
while (!sr.EndOfStream)
{
Debug.Log("第"+i+"行+"+sr.ReadLine());
i++;
}
sr.Close();
}
Debug.Log("读取成功");
}
}
网友评论