如何读取和写入到文本文件中,通过使用 Visual C#
来源:互联网 发布:关于网络的标语 编辑:程序博客网 时间:2024/05/16 11:52
读取和写入文本文件
Read a Text File 本文一节描述如何使用 StreamReader 类来读取文本文件。 Write a Text File (Example 1) 和 Write a Text File (Example 2) 各节描述了如何使用 StreamWriter 类向文件写入文本。
读取文本文件
下面的代码使用 StreamReader 类打开、 读取,并关闭该文本文件。 您可以将文本文件的路径传递给要自动打开文件 StreamReader 构造函数。 ReadLine 方法读取每行文本,并根据它读取递增到下一行将文件指针。 ReadLine 方法到达文件结尾时, 它将返回空引用。
- 在记事本中创建示例文本文件。 若要执行此操作,请按照下列步骤操作:
- 在记事本中粘贴以下文本:
hello world
- 将文件另存为 Sample.txt。
- 在记事本中粘贴以下文本:
- 启动 Microsoft Visual Studio。
- 在 文件 菜单上指向 新建,然后单击 项目。
- 在 项目类型,下单击 Visual C# 项目,然后单击 模板 下的 控制台应用程序
注意 在 Visual Studio 2005 或 Visual Studio 2008,单击 Visual C#项目类型,下,然后单击 模板 下的 控制台应用程序。 - Class1.cs 文件的开头添加以下代码:注意 在 Visual Studio 2005 或 Visual Studio 2008,默认的文件是 Program.cs。
using System.IO;
- 下面的代码添加到 Main 方法:块。
String line;
try
{
//Pass the file path and file name to the StreamReader constructor
StreamReader sr = new StreamReader("C://Sample.txt");
//Read the first line of text
line = sr.ReadLine();
//Continue to read until you reach end of file
while (line != null)
{
//write the lie to console window
Console.WriteLine(line);
//Read the next line
line = sr.ReadLine();
}
//close the file
sr.Close();
Console.ReadLine();
}
catch(Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
Console.WriteLine("Executing finally block.");
} - 在 调试 菜单上单击 开始 编译并运行该应用程序。 按 ENTER 关闭控制台窗口。 控制台窗口将显示在 Sample.txt 文件中的内容
Hello world
写文本文件 (示例 1)
下面的代码使用 StreamWriter 类打开编写,并关闭该文本文件。 StreamReader 类以类似方式,您可以将文本文件的路径传递给要自动打开文件 StreamWriter 构造函数。 WriteLine 方法将一个完整的文本行写入文本文件。
- 启动 Visual Studio。
- 在 文件 菜单上指向 新建,然后单击 项目。
- 在 项目类型,下单击 Visual C# 项目,然后单击 模板 下的 控制台应用程序。
注意 在 Visual Studio 2005 或 Visual Studio 2008,单击 Visual C#项目类型,下,然后单击 模板 下的 CLR 控制台应用程序。 - Class1.cs 文件的开头添加以下代码:
using System.IO;
- 下面的代码添加到 Main 方法:块。
try
{
//Pass the filepath and filename to the StreamWriter Constructor
StreamWriter sw = new StreamWriter("C://Test.txt");
//Write a line of text
sw.WriteLine("Hello World!!");
//Write a second line of text
sw.WriteLine("From the StreamWriter class");
//Close the file
sw.Close();
}
catch(Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
Console.WriteLine("Executing finally block.");
} - 在 调试 菜单上单击 开始 编译并运行该应用程序。 此代码创建名为 Test.txt 椹卞姩鍣 ㄤ 笂 C.打开 Test.txt 在文本编辑器 (如记事本) 中的文件。 Test.txt 包含两行文本:
Hello World!!
From the StreamWriter class
写文本文件 (示例 2)
下面的代码使用 StreamWriter 类打开编写,并关闭该文本文件。 与前面的示例不同此代码将两个附加参数传递给构造函数。 第一个参数是文件路径和文件的文件名。 第二个参数 为 True,指定打开该文件中追加模式。 如果在您第二个参数指定 False,文件的内容将覆盖每次运行该代码。 第三个参数指定 Unicode,以便 StreamWriter Unicode 格式文件进行编码。 您还可以指定下列编码方法对于第三个参数:
- ASC11
- Unicode
- UTF7
- UTF8
Write 方法与 WriteLine 方法的类似之处在于 Write 方法不会自动嵌入回车或换行 (CR/LF) 字符组合。 当您希望一次写入一个字符时,这是很有用。
- 启动 Visual Studio。
- 在 文件 菜单上指向 新建,然后单击 项目。
- 在 项目类型,下单击 Visual C# 项目,然后单击 模板 下的 控制台应用程序
注意 在 Visual Studio 2005 或 Visual Studio 2008,单击 Visual C#项目类型,下,然后单击 模板 下的 控制台应用程序 - Class1.cs 文件的开头添加以下代码:注意 在 Visual Studio 2005 或 Visual Studio 2008,默认的文件是 Program.cs。
using System.IO;
using System.Text; - 下面的代码添加到 Main 方法:
Int64 x;
try
{
//Open the File
StreamWriter sw = new StreamWriter("C://Test1.txt", true, Encoding.ASCII);
//Writeout the numbers 1 to 10 on the same line.
for(x=0; x < 10; x++)
{
sw.Write(x);
}
//close the file
sw.Close();
}
catch(Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
Console.WriteLine("Executing finally block.");
} - 在 调试 菜单上单击 开始 编译并运行该应用程序。 此代码创建名为 Test1.txt 椹卞姩鍣 ㄤ 笂 C.打开 Test1.txt 在文本编辑器 (如记事本) 中的文件。 Test1.txt 包含单行文本:
0123456789
瀹屾暣浠 g 爜鍒楄 〃
- 读取文本文件块。
//Read a Text File
using System;
using System.IO;
namespace readwriteapp
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
String line;
try
{
//Pass the file path and file name to the StreamReader constructor
StreamReader sr = new StreamReader("C://Sample.txt");
//Read the first line of text
line = sr.ReadLine();
//Continue to read until you reach end of file
while (line != null)
{
//write the lie to console window
Console.WriteLine(line);
//Read the next line
line = sr.ReadLine();
}
//close the file
sr.Close();
Console.ReadLine();
}
catch(Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
Console.WriteLine("Executing finally block.");
}
}
}
} - 写文本文件 (版本 1)块。
//Write a text file - Version-1
using System;
using System.IO;
namespace readwriteapp
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
try
{
//Pass the filepath and filename to the StreamWriter Constructor
StreamWriter sw = new StreamWriter("C://Test.txt");
//Write a line of text
sw.WriteLine("Hello World!!");
//Write a second line of text
sw.WriteLine("From the StreamWriter class");
//Close the file
sw.Close();
}
catch(Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
Console.WriteLine("Executing finally block.");
}
}
}
} - 写文本文件 (版本 2)
//Write a text file - Version 2
using System;
using System.IO;
using System.Text;
namespace readwriteapp
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Int64 x;
try
{
//Open the File
StreamWriter sw = new StreamWriter("C://Test1.txt", true, Encoding.ASCII);
//Writeout the numbers 1 to 10 on the same line.
for(x=0; x < 10; x++)
{
sw.Write(x);
}
//close the file
sw.Close();
}
catch(Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
Console.WriteLine("Executing finally block.");
}
}
}
}
疑难解答
对于所有的文件操作是好的编程习惯自动换行内 尝试-catch-finally 块来处理错误和异常代码。 专门,您可能希望释放最后块中文件的句柄,使该文件不无限期锁定。 一些可能的错误包括一个文件不存在或已在使用中的文件。
【有关详细信息请访问下面的 Microsoft 开发人员网络 (MSDN) 的网站:
http://msdn2.microsoft.com/en-us/library/system.io.streamreader(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/system.io.streamreader(vs.71).aspx)
】
- 如何读取和写入到文本文件中,通过使用 Visual C#
- C#文本文件读取和写入
- C#文本文件读取和写入
- 使用 System.IO 和 Visual C# 读取文本文件
- c#读取写入文本文件
- c#读取写入文本文件
- 如何使用C#读取文本文件
- 如何使用C#读取文本文件
- 文本文件读取和写入
- C#读取 文本文件 写入textbox
- 【MATLAB】读取和写入文本文件
- HOW TO:使用 System.IO 和 Visual C# .NET 读取文本文件
- 使用StreamReader和StreamWriter读取和写入文本文件
- 使用C#如何写入/读取注册表信息
- 使用C#如何写入/读取注册表信息
- C#写入和读出文本文件
- C#写入和读出文本文件
- C#写入和读出文本文件
- 原型模式(Prototype Pattern)
- 感悟
- VS2008序列号CDKEY:PYHYP-WXB3B-B2CCM-V9DX9-VDY8T
- .cn的特色是怎样“炼”成的?
- Windows Mobile中的文件改变通知
- 如何读取和写入到文本文件中,通过使用 Visual C#
- C++内存管理基础之new & delete
- 完美代码,让你的代码无懈可击
- 今天开博了!
- 浅谈C与C++的设计与编程风格(一)
- UCOS-II初始化(1)
- 一位超算中心管理人员的空间
- 谷歌与百度之别
- 天书夜读(1)--Windows内核编程系列读书笔记(一)