.NET写入文本文件的操作是如何实现的呢?下面我们通过使用VB和C#两种代码示例向你演示如何实现写入文本文件的操作细节,希望对你了解写入文本文件有所帮助
创新互联建站科技有限公司专业互联网基础服务商,为您提供四川主机托管,高防主机,成都IDC机房托管,成都主机托管等互联网服务。
***个示例演示如何向现有文件中添加文本。第二个示例演示如何创建一个新文本文件并向其中写入一个字符串。 WriteAllText 方法可提供类似的功能。
.NET写入文本文件的操作时需要注意注意
Visual Basic 用户可以选择使用由 My.Computer.FileSystem 对象提供的方法和属性进行文件 I/O。有关更多信息,请参见 My.Computer.FileSystem 对象。
.NET写入文本文件的操作示例Visual Basic实现添加文本
- 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#实现添加文本
- 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实现写入一个字符串
- 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#实现写入一个字符串
- 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写入文本文件的操作示例的基本实现就向你介绍到这里,希望对你了解.NET写入文本文件的操作有所帮助。
本文标题:.NET写入文本文件的操作浅析
网站路径:http://www.shufengxianlan.com/qtweb/news45/259445.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联