如何:写入文本文件(C# 编程指南)

本文通过四个示例展示了如何使用C#的System.IO..::.File类进行文本文件的操作,包括写入完整字符串数组、单个字符串、部分字符串以及追加新文本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

以下示例给出了将文本写入文件的各种方法。 前两个示例使用 System.IO..::.File 类中的静态方法将完整的字符串数组或完整字符串写入文本文件。 示例 #3 说明如果在将文本写入文件之前必须分别处理文本的每一行,应该如何将这些文本添加到文件中。 示例 1-3 均覆盖文件中的所有现有内容。 示例 #4 说明如何将文本追加到现有文件的末尾。

示例
  1.     class WriteTextFile
  2.     {
  3.         static void Main()
  4.         {
  5.  
  6.             // These examples assume a "C:\Users\Public\TestFolder" folder on your machine.
  7.             // You can modify the path if necessary.
  8.  
  9.             // Example #1: Write an array of strings to a file.
  10.             // Create a string array that consists of three lines.
  11.             string[] lines = {"First line""Second line""Third line"};
  12.             System.IO.File.WriteAllLines(@"C:\Users\Public\TestFolder\WriteLines.txt", lines);
  13.  
  14.  
  15.             // Example #2: Write one string to a text file.
  16.             string text = "A class is the most powerful data type in C#. Like structures, " +
  17.                            "a class defines the data and behavior of the data type. ";
  18.             System.IO.File.WriteAllText(@"C:\Users\Public\TestFolder\WriteText.txt", text);
  19.  
  20.             // Example #3: Write only some strings in an array to a file.
  21.             using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Public\TestFolder\WriteLines2.txt"))
  22.             {
  23.                 foreach (string line in lines)
  24.                 {
  25.                     if (line.Contains("Second") == false)
  26.                     {
  27.                         file.WriteLine(line);
  28.                     }
  29.                 }
  30.             }
  31.  
  32.             // Example #4: Append new text to an existing file
  33.             using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Public\TestFolder\WriteLines2.txt"true))
  34.             {
  35.                 file.WriteLine("Fourth line");
  36.             }  
  37.         }
  38.     }
  39.     /* Output (to WriteLines.txt):
  40.         First line
  41.         Second line
  42.         Third line
  43.      Output (to WriteText.txt):
  44.         A class is the most powerful data type in C#. Like structures, a class defines the data and behavior of the data type.
  45.      Output to WriteLines2.txt after Example #3:
  46.         First line
  47.         Third line
  48.      Output to WriteLines2.txt after Example #4:
  49.         First line
  50.         Third line
  51.         Fourth line
  52.      */
  53.  
  54.  
编译代码

将代码复制到控制台应用程序中。

"c:\testdir" 替换为您计算机上的实际文件夹名称,或者创建一个此名称的文件夹。

可靠编程

以下情况可能会导致异常:

  • 文件已存在并且为只读。

  • 路径名可能太长。

  • 磁盘可能已满。

转载于:https://www.cnblogs.com/OnlyYou/archive/2011/11/23/2260348.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值