繁体   English   中英

如何使用C#在Unity中使用分隔行将文本保存到文件

[英]How to save text to file with the separated lines in Unity with c#

当我尝试在编辑器中保存一些文本时,当我想在Android设备中的分隔行中保存一些文本时得到了result1 ,但是我得到了类似result2的结果

result 1 is 
a
b
c
d

result 2 is
abcd 

这是我的代码

public void savetofile()
{
    StreamWriter SW = new StreamWriter(" path to file "); 
    SW.WriteLine("a");
    SW.WriteLine("b");
    SW.WriteLine("c");
    SW.WriteLine("d");
}

根据WriteLine文档

将字符串后跟行终止符写入文本字符串或流。

每行的末尾应打印换行符,但是如示例所示,这不会发生。 有人可以告诉我怎么了吗?

您可以将控制代码“ \\ n”用于新行。

public void savetofile()
{
    StreamWriter SW = new StreamWriter(" path to file "); 
    SW.WriteLine("a");
    SW.WriteLine("/n");
    SW.WriteLine("b");
    SW.WriteLine("/n");
    SW.WriteLine("c");
    SW.WriteLine("/n");
    SW.WriteLine("d");
}

我认为您可能想尝试声明一个字符串,而不是保存它,否则可能会发生提示。

StreamWriter SW = new StreamWriter(" path to file "); 
string s = "a 
            b  
            c 
            d";   
SW.WriteLine(s);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM