Txt文档读写《二》

使用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("读取成功");

        }
    }

猜你喜欢

转载自blog.csdn.net/weixin_33769125/article/details/86790065