自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

.NET寫入文本文件的操作淺析

開發(fā) 后端
.NET寫入文本文件的操作是什么呢?我們會碰到如何實現(xiàn)添加文本或是添加字符串的操作,那么本文就向你介紹具體的實現(xiàn)過程以及注意事項。

.NET寫入文本文件的操作是如何實現(xiàn)的呢?下面我們通過使用VB和C#兩種代碼示例向你演示如何實現(xiàn)寫入文本文件的操作細節(jié),希望對你了解寫入文本文件有所幫助

***個示例演示如何向現(xiàn)有文件中添加文本。第二個示例演示如何創(chuàng)建一個新文本文件并向其中寫入一個字符串。 WriteAllText 方法可提供類似的功能。

.NET寫入文本文件的操作時需要注意注意
 
Visual Basic 用戶可以選擇使用由 My.Computer.FileSystem 對象提供的方法和屬性進行文件 I/O。有關(guān)更多信息,請參見 My.Computer.FileSystem 對象。

.NET寫入文本文件的操作示例Visual Basic實現(xiàn)添加文本

  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#實現(xiàn)添加文本

  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實現(xiàn)寫入一個字符串

  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#實現(xiàn)寫入一個字符串

  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寫入文本文件的操作示例的基本實現(xiàn)就向你介紹到這里,希望對你了解.NET寫入文本文件的操作有所幫助。

【編輯推薦】

  1. 淺析C#FileStream寫文件的操作
  2. C#StreamWriter的操作解析
  3. C#BinaryWriter的使用淺析
  4. C#緩存流的使用淺析
  5. C#內(nèi)存流的使用實例探討
責任編輯:仲衡 來源: MSDN
相關(guān)推薦

2009-08-19 17:44:15

C#操作文本文件

2009-08-20 09:15:20

C#操作文本文件

2009-09-02 19:13:08

C#處理文本文件

2009-08-20 10:17:27

C#操作文本文件

2021-11-29 09:46:11

FileReaderJava開發(fā)

2009-10-29 14:16:32

VB.NET讀寫文本文

2009-09-02 19:08:03

C#實現(xiàn)讀取文本文件

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#操作文本文件

2010-01-15 16:21:45

VB.NET讀寫文本文

2015-06-17 14:28:15

Java查詢處理方法

2009-08-26 11:53:56

C#打印文本文件

2010-05-13 17:43:43

IIS服務(wù)器

2014-03-11 10:11:33

Linux命令more命令文本文件
點贊
收藏

51CTO技術(shù)棧公眾號