Unity C# 程序中的 StreamWriter

StreamWriter in a Unity C# program

为了让我的 Unity 应用程序向文件写入内容,我编写了以下代码:

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

public class StreamManager : MonoBehaviour
{
    void Start ()
    {
        WriteToFile();
    }

    void Update ()
    {}

    public static void WriteToFile()
    {
        StreamWriter sw = new StreamWriter( Application.persistentDataPath + "table.txt" );

        sw.WriteLine( "Generated table of 1 to 10" );
        sw.WriteLine( "" );

        for ( int i = 1; i <= 10; i++ )
        {
            for ( int j = 1; j <= 10; j++ )
            {
                sw.WriteLine( "{0}x{1}= {2}", i, j, (i*j) );
            }

            sw.WriteLine( "====================================" );
        }

        Console.WriteLine( "Table successfully written to file!" );

        sw.Close();
    }
}

但是,最后一行(Table成功写入文件!)没有写入文本文件。

这是为什么?我在这里错过了什么?

谢谢,

你把它写到控制台输出,而不是文件

Console.WriteLine( "Table successfully written to file!" );

这个有效:

sw.WriteLine( "Table successfully written to file!" );

使用:

sw.WriteLine( "Table successfully written to file!" );

而不是

Console.WriteLine( "Table successfully written to file!" );