程序开发 > C# > 正文

C#写文本文件,如何换行(添加换行符)Environment.NewLine,读取一行显示为指定字数的多行

亮术网 2020-03-02 本网原创

把文本写到文件中,如果是几段文字拼合起来输出到文件中,通常每段非结尾文字后需要添加换行符,不然几段文字都变成一段。如果文本文件中的所有内容为一行,读取到文本框也会是一行,反之宜然;若要求把它们分为多行,每行显示指定字数,需要在每行后添加换行符。

在 C# 中,文本换行有两种方法,一种在需要换行的文本后面添加换行符 \r\n 即可,另一种方法用 Environment.NewLine,功能与 \r\n 一样,接着分别看这种两种方法的具体实现代码。

 

一、C# 写文本文件换行

1、C#文本换行之用换行符 \r\n

一个 \r\n 表示换一行;如果要换两行,则重复 \r\n,即 \r\n\r\n;换三行重复三次,即 \r\n\r\n\r\n;往下依次类推。代码示例如下:

using System.IO;

/// <summary>
  /// C#写文本换行
  /// </summary>
  /// <param name="filePath">文本文件路径</param>

  public void SaveTxtFile(string filePath)
  {
    string text = "C#文本换行" + :"\r\n" + "写文本文件换行符";//\r\n表示换一行
    try
    {
      using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write))
      {
        StreamWriter sw = new StreamWriter(fs);
        sw.Write(text);
        sw.Flush();
        sw.Close();
        fs.Close();
      }
    }
    catch
    {
      //"保存文本文件出错!"
    }
  }

调用方法:SaveTxtFile( @"F:\temp\txtName.txt");

 

 

2、C#文本换行之用 Environment.NewLine

下面的代码也是写文本文件,实现方法跟上面的相同,只是用 Environment.NewLine 代替 \r\n,具体代码如下:

using System.IO;

/// <summary>
  /// C# 用 Environment.NewLine 换行
  /// </summary>
  /// <param name="filePath">文本文件路径</param>

  public void SaveTxtFile(string filePath)
  {
    try
    {
      using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write))
      {
        StreamWriter sw = new StreamWriter(fs);
        sw.BaseStream.Seek(0, SeekOrigin.End);
        sw.WriteLine("用 Environment.NewLine 实现文本文件换行;");
        sw.WriteLine(Environment.NewLine);//换行
        sw.WriteLine("被换行段落。");
        sw.Flush();
        sw.Close();
        fs.Close();
      }
    }
    catch
    {
      //"保存文本文件出错!"
    }
  }

调用方法:SaveTxtFile( @"F:\temp\txtName.txt");

 

3、C# Textbox 的内容保存到txt文件后不换行

如果把 Textbox 文本框中的内容保存到文本文件不换行,是因为 Textbox 的换行符为 \n,而文本文件的换行符为 \r\n,只有把 \n 替换为 \r\n,才会换行,代码如下:

using System.IO;

private void SaveTextToTxtFile(string filePath)
  {
    string text = textbox1.Text;
    if (string.IsNullOrEmpty(text))
       return;

   text = text.Replace("\n", "\r\n");
     File.WriteAllText(filePath, text);
  }

调用方法:SaveTextToTxtFile(@"F:\temp\txtName.txt");

 

二、C# 读取文本文件换行

1、C# 把一行显示为多行

如果文本文件中的内容为一行,要求读出来显示为多行,每行显示 20 个字,代码如下:

using System.IO;

private void ReadTextFromTxtFile(string filePath)
  {
     try
     {
       using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
       {
       StreamReader sr = new StreamReader(fs);
       string line = sr.ReadLine(), text = line;
       while (line != null)
       {
         line = sr.ReadLine();
         text += line;
       }
       sr.Close();
       fs.Close();
       textbox1.Text = RichTextBoxDisplayNLines(text, 1000, 20);
       }
     }
     catch
     {
       //读取文本文件出错提示!
     }
  }

说明:RichTextBoxDisplayNLines 方法在《C# Richtextbox 创建、设置字体颜色、添加删除读取行、每行显示指定字数、修改选中文字颜色、用红色标出行》一文。

调用:ReadTextFromTxtFile(@"F:\temp\txtName.txt");

 

2、C# 分号后换行

从文本文件读取内容,如果要求在每个分号后换行,需要在分号加换行符 \n,代码如下:

private void ReadTextFromTxtFileNewlineFromSemicolon(string filePath)
  {
    try
    {
     using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
     {
       StreamReader sr = new StreamReader(fs);
       string text = sr.ReadToEnd();
 
       sr.Close();
       fs.Close();
       if (!string.IsNullOrEmpty(text))
        textbox1.Text = text.Replace(";", ";\r\n");
     }
    }
    catch
    {
      //读取文本文件出错提示!
    }
  }

调用:ReadTextFromTxtFileNewlineFromSemicolon(@"F:\temp\txtName.txt");