如何使用StreamWriter.WriteLine 方法在C# 中编写unix endofline 终止符'\n'?
How to use StreamWriter.WriteLine method to write unix endofline terminator '\n' in C#?
StreamWriter.WriteLine
自动写入行尾终止符,但是 StreamWriter.WriteLine
方法的默认行尾终止符似乎是 \r\n
,是否可以将其更改为 \n
for unix 平台?
我是运行Windows上的代码 10、平台相关吗?我可以在 Windows 环境中使用 StreamWriter.WriteLine
输出带有 unix 行尾终止符 \n
的文本文件吗?
这是我的代码:
using System;
using System.IO;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
StreamWriter sw = new StreamWriter("NewLineText.txt", true);
for (int i = 0; i < 3; i++)
{
sw.WriteLine("new line test" + i);
}
sw.Close();
}
}
}
我建议你使用 Write
而不是 WriteLine
或者写一个扩展方法。
public static class UnixStreamWriter
{
public static void WriteLineUnix(this StreamWriter writer, string value)
{
writer.Write(value+"\n");
}
}
然后你可以这样称呼它,
StreamWriter sw = new StreamWriter("NewLineText.txt", true);
for (int i = 0; i < 3; i++)
{
sw.WriteLineUnix("new line test" + i);
}
sw.Close();
StreamWriter.WriteLine write endofline terminator automatically, but It seems that the default endofline terminator for StreamWriter.WriteLine method is '\r\n', is it possible to change it to '\n' for unix platform?
在 Unix 上 运行 时它已经使用 \n
。
I'm running the code on Windows 10, Is it platform-related?
是的。
Can I use StreamWriter.WriteLine to output a textfile with unix endofline terminator '\n' on Windows environment?
是 - 通过更改 NewLine
属性。您需要做的就是在调用 WriteLine
:
之前设置 属性
StreamWriter sw = new StreamWriter("NewLineText.txt", true);
sw.NewLine = "\n"; // Use Unix line endings
// Other code as before
顺便说一句,我还建议使用 using
语句,以便适当地处理作者 - 我个人通常会使用 File.AppendText
或 File.CreateText
。所以:
using var writer = File.AppendText("NewLineText.txt");
writer.NewLine = "\n"; // Use Unix line endings
for (int i = 0; i < 3; i++)
{
writer.WriteLine($"new line test {i}");
}
StreamWriter.WriteLine
自动写入行尾终止符,但是 StreamWriter.WriteLine
方法的默认行尾终止符似乎是 \r\n
,是否可以将其更改为 \n
for unix 平台?
我是运行Windows上的代码 10、平台相关吗?我可以在 Windows 环境中使用 StreamWriter.WriteLine
输出带有 unix 行尾终止符 \n
的文本文件吗?
这是我的代码:
using System;
using System.IO;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
StreamWriter sw = new StreamWriter("NewLineText.txt", true);
for (int i = 0; i < 3; i++)
{
sw.WriteLine("new line test" + i);
}
sw.Close();
}
}
}
我建议你使用 Write
而不是 WriteLine
或者写一个扩展方法。
public static class UnixStreamWriter
{
public static void WriteLineUnix(this StreamWriter writer, string value)
{
writer.Write(value+"\n");
}
}
然后你可以这样称呼它,
StreamWriter sw = new StreamWriter("NewLineText.txt", true);
for (int i = 0; i < 3; i++)
{
sw.WriteLineUnix("new line test" + i);
}
sw.Close();
StreamWriter.WriteLine write endofline terminator automatically, but It seems that the default endofline terminator for StreamWriter.WriteLine method is '\r\n', is it possible to change it to '\n' for unix platform?
在 Unix 上 运行 时它已经使用 \n
。
I'm running the code on Windows 10, Is it platform-related?
是的。
Can I use StreamWriter.WriteLine to output a textfile with unix endofline terminator '\n' on Windows environment?
是 - 通过更改 NewLine
属性。您需要做的就是在调用 WriteLine
:
StreamWriter sw = new StreamWriter("NewLineText.txt", true);
sw.NewLine = "\n"; // Use Unix line endings
// Other code as before
顺便说一句,我还建议使用 using
语句,以便适当地处理作者 - 我个人通常会使用 File.AppendText
或 File.CreateText
。所以:
using var writer = File.AppendText("NewLineText.txt");
writer.NewLine = "\n"; // Use Unix line endings
for (int i = 0; i < 3; i++)
{
writer.WriteLine($"new line test {i}");
}