.NET写入文本文件的操作浅析

开发 后端
.NET写入文本文件的操作是什么呢?我们会碰到如何实现添加文本或是添加字符串的操作,那么本文就向你介绍具体的实现过程以及注意事项。

.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#内存流的使用实例探讨
责任编辑:仲衡 来源: MSDN
相关推荐

2009-08-20 09:15:20

C#操作文本文件

2009-08-19 17:44:15

C#操作文本文件

2009-08-20 10:17:27

C#操作文本文件

2009-09-02 19:13:08

C#处理文本文件

2021-11-29 09:46:11

FileReaderJava开发

2009-09-02 19:08:03

C#实现读取文本文件

2009-10-29 14:16:32

VB.NET读写文本文

2010-01-11 17:05:32

VB.NET操作文本文

2009-11-02 11:22:59

VB.NET文本文件操

2010-01-15 10:05:35

VB.NET文件对象

2010-04-30 17:38:31

Unix文本

2009-10-14 10:25:52

VB.NET读写文本文

2010-01-08 16:10:05

VB.NET读写文本文

2009-08-06 18:33:45

C#处理文本文件

2009-08-20 09:58:06

C#操作文本文件

2015-06-17 14:28:15

Java查询处理方法

2010-01-15 16:21:45

VB.NET读写文本文

2009-08-26 11:53:56

C#打印文本文件

2010-05-13 17:43:43

IIS服务器

2014-03-11 10:11:33

Linux命令more命令文本文件
点赞
收藏

51CTO技术栈公众号