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

創(chuàng)新互聯(lián)建站科技有限公司專業(yè)互聯(lián)網(wǎng)基礎(chǔ)服務(wù)商,為您提供四川主機(jī)托管,高防主機(jī),成都IDC機(jī)房托管,成都主機(jī)托管等互聯(lián)網(wǎng)服務(wù)。
***個(gè)示例演示如何向現(xiàn)有文件中添加文本。第二個(gè)示例演示如何創(chuàng)建一個(gè)新文本文件并向其中寫入一個(gè)字符串。 WriteAllText 方法可提供類似的功能。
.NET寫入文本文件的操作時(shí)需要注意注意
Visual Basic 用戶可以選擇使用由 My.Computer.FileSystem 對(duì)象提供的方法和屬性進(jìn)行文件 I/O。有關(guān)更多信息,請(qǐng)參見 My.Computer.FileSystem 對(duì)象。
.NET寫入文本文件的操作示例Visual Basic實(shí)現(xiàn)添加文本
- Imports System
- Imports System.IO
- Class Test
- Public Shared Sub Main()
- ' Create an instance of StreamWriter to write text to a file.
- Using sw As StreamWriter = New StreamWriter("TestFile.txt")
- ' Add some text to the file.
- sw.Write("This is the ")
- sw.WriteLine("header for the file.")
- sw.WriteLine("-------------------")
- ' Arbitrary objects can also be written to the file.
- sw.Write("The date is: ")
- sw.WriteLine(DateTime.Now)
- sw.Close()
- End Using
- End Sub
- End Class
.NET寫入文本文件的操作示例C#實(shí)現(xiàn)添加文本
- using System;
- using System.IO;
- class Test
- {
- public static void Main()
- {
- // Create an instance of StreamWriter to write text to a file.
- // The using statement also closes the StreamWriter.
- using (StreamWriter sw = new StreamWriter("TestFile.txt"))
- {
- // Add some text to the file.
- sw.Write("This is the ");
- sw.WriteLine("header for the file.");
- sw.WriteLine("-------------------");
- // Arbitrary objects can also be written to the file.
- sw.Write("The date is: ");
- sw.WriteLine(DateTime.Now);
- }
- }
- }
.NET寫入文本文件的操作示例Visual Basic實(shí)現(xiàn)寫入一個(gè)字符串
- Option Explicit On
- Option Strict On
- Imports System
- Imports System.IO
- Public Class TextToFile
- Private Const FILE_NAME As String = "MyFile.txt"
- Public Shared Sub Main()
- If File.Exists(FILE_NAME) Then
- Console.WriteLine("{0} already exists.", FILE_NAME)
- Return
- End If
- Using sw As StreamWriter = File.CreateText(FILE_NAME)
- sw.WriteLine("This is my file.")
- sw.WriteLine("I can write ints {0} or floats {1}, and so on.", 1, 4.2)
- sw.Close()
- End Using
- End Sub
- End Class
.NET寫入文本文件的操作示例C#實(shí)現(xiàn)寫入一個(gè)字符串
- using System;
- using System.IO;
- public class TextToFile
- {
- private const string FILE_NAME = "MyFile.txt";
- public static void Main(String[] args)
- {
- if (File.Exists(FILE_NAME))
- {
- Console.WriteLine("{0} already exists.", FILE_NAME);
- return;
- }
- using (StreamWriter sw = File.CreateText(FILE_NAME))
- {
- sw.WriteLine ("This is my file.");
- sw.WriteLine ("I can write ints {0} or floats {1}, and so on.",
- 1, 4.2);
- sw.Close();
- }
- }
- }
.NET寫入文本文件的操作示例的基本實(shí)現(xiàn)就向你介紹到這里,希望對(duì)你了解.NET寫入文本文件的操作有所幫助。
網(wǎng)頁題目:.NET寫入文本文件的操作淺析
當(dāng)前路徑:http://www.dlmjj.cn/article/dpdgiod.html


咨詢
建站咨詢
