using UnityEngine;
using System.Collections;
using System.IO;
public class Script_08_02 : MonoBehaviour
{
void Start ()
{
Debug.Log(Application.dataPath);
CreateFile(Application.dataPath, "FileName","TestInfo0");
CreateFile(Application.dataPath, "FileName", "TestInfo1");
CreateFile(Application.dataPath, "FileName", "TestInfo2");
}
/// <summary>
/// 创建一个文件
/// </summary>
/// <param name="path">路径</param>
/// <param name="name">名称</param>
/// <param name="info">写入的内容</param>
void CreateFile(string path, string name, string info)
{
// 文件流信息
StreamWriter sw;
FileInfo t = new FileInfo(path + "//" + name);
if (!t.Exists)
{
// 如果文件不存在则创建
sw = t.CreateText();
}
else
{
// 如果文件存在,则打开该文件
sw = t.AppendText();
}
// 以行的形式写入信息
sw.WriteLine(info);
// 关闭流
sw.Close();
// 销毁流
sw.Dispose();
}
}
using System.Collections;
using System.IO;
public class Script_08_02 : MonoBehaviour
{
void Start ()
{
Debug.Log(Application.dataPath);
CreateFile(Application.dataPath, "FileName","TestInfo0");
CreateFile(Application.dataPath, "FileName", "TestInfo1");
CreateFile(Application.dataPath, "FileName", "TestInfo2");
}
/// <summary>
/// 创建一个文件
/// </summary>
/// <param name="path">路径</param>
/// <param name="name">名称</param>
/// <param name="info">写入的内容</param>
void CreateFile(string path, string name, string info)
{
// 文件流信息
StreamWriter sw;
FileInfo t = new FileInfo(path + "//" + name);
if (!t.Exists)
{
// 如果文件不存在则创建
sw = t.CreateText();
}
else
{
// 如果文件存在,则打开该文件
sw = t.AppendText();
}
// 以行的形式写入信息
sw.WriteLine(info);
// 关闭流
sw.Close();
// 销毁流
sw.Dispose();
}
}
本文介绍了一个使用Unity进行文件创建及写入操作的具体示例。通过示例代码展示了如何根据给定路径创建文件,并向文件中写入指定内容。
4万+

被折叠的 条评论
为什么被折叠?



