.NET写入文本文件的操作浅析
创始人
2024-06-06 06:20:26
0

.NET写入文本文件的操作是如何实现的呢?下面我们通过使用VB和C#两种代码示例向你演示如何实现写入文本文件的操作细节,希望对你了解写入文本文件有所帮助

***个示例演示如何向现有文件中添加文本。第二个示例演示如何创建一个新文本文件并向其中写入一个字符串。 WriteAllText 方法可提供类似的功能。

.NET写入文本文件的操作时需要注意注意
 
Visual Basic 用户可以选择使用由 My.Computer.FileSystem 对象提供的方法和属性进行文件 I/O。有关更多信息,请参见 My.Computer.FileSystem 对象。

.NET写入文本文件的操作示例Visual Basic实现添加文本

  1. Imports System  
  2. Imports System.IO  
  3.  
  4. Class Test  
  5. Public Shared Sub Main()  
  6. ' Create an instance of StreamWriter to write text to a file.  
  7. Using sw As StreamWriter = New StreamWriter("TestFile.txt")  
  8. ' Add some text to the file.  
  9. sw.Write("This is the ")  
  10. sw.WriteLine("header for the file.")  
  11. sw.WriteLine("-------------------")  
  12. ' Arbitrary objects can also be written to the file.  
  13. sw.Write("The date is: ")  
  14. sw.WriteLine(DateTime.Now)  
  15. sw.Close()  
  16. End Using  
  17. End Sub  
  18. End Class  

.NET写入文本文件的操作示例C#实现添加文本

  1. using System;  
  2. using System.IO;  
  3.  
  4. class Test   
  5. {  
  6. public static void Main()   
  7. {  
  8. // Create an instance of StreamWriter to write text to a file.  
  9. // The using statement also closes the StreamWriter.  
  10. using (StreamWriter sw = new StreamWriter("TestFile.txt"))   
  11. {  
  12. // Add some text to the file.  
  13. sw.Write("This is the ");  
  14. sw.WriteLine("header for the file.");  
  15. sw.WriteLine("-------------------");  
  16. // Arbitrary objects can also be written to the file.  
  17. sw.Write("The date is: ");  
  18. sw.WriteLine(DateTime.Now);  
  19. }  
  20. }  
  21. }  

.NET写入文本文件的操作示例Visual Basic实现写入一个字符串

  1. Option Explicit On   
  2. Option Strict On  
  3. Imports System  
  4. Imports System.IO  
  5. Public Class TextToFile  
  6. Private Const FILE_NAME As String = "MyFile.txt" 
  7. Public Shared Sub Main()  
  8. If File.Exists(FILE_NAME) Then  
  9. Console.WriteLine("{0} already exists.", FILE_NAME)  
  10. Return  
  11. End If  
  12. Using sw As StreamWriter = File.CreateText(FILE_NAME)  
  13. sw.WriteLine("This is my file.")  
  14. sw.WriteLine("I can write ints {0} or floats {1}, and so on.", 1, 4.2)  
  15. sw.Close()  
  16. End Using  
  17. End Sub  
  18. End Class 

.NET写入文本文件的操作示例C#实现写入一个字符串

  1. using System;  
  2. using System.IO;  
  3. public class TextToFile   
  4. {  
  5. private const string FILE_NAME = "MyFile.txt";  
  6. public static void Main(String[] args)   
  7. {  
  8. if (File.Exists(FILE_NAME))   
  9. {  
  10. Console.WriteLine("{0} already exists.", FILE_NAME);  
  11. return;  
  12. }  
  13. using (StreamWriter sw = File.CreateText(FILE_NAME))  
  14. {  
  15. sw.WriteLine ("This is my file.");  
  16. sw.WriteLine ("I can write ints {0} or floats {1}, and so on.",   
  17. 1, 4.2);  
  18. sw.Close();  
  19. }  
  20. }  

.NET写入文本文件的操作示例的基本实现就向你介绍到这里,希望对你了解.NET写入文本文件的操作有所帮助。

【编辑推荐】

  1. 浅析C#FileStream写文件的操作
  2. C#StreamWriter的操作解析
  3. C#BinaryWriter的使用浅析
  4. C#缓存流的使用浅析
  5. C#内存流的使用实例探讨

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...