要求
下面的列表概述了建议的硬件、软件、网络的基础结构和服务包所必需的:
?visual C#
本文假定您熟悉下列主题:
?visual C#
回到顶端
读取和写入文本文件
Read a Text File的这篇文章部分描述如何使用StreamReader类来读取文本的文件。Write a Text File (Example 1),和Write a Text File (Example 2)在各节说明了如何使用StreamWriter 类来向文件写入文本。
读取文本文件
若要打开、读取,和来关闭文本文件,下面的代码使用StreamReader类。您可以将文本文件的路径传递给StreamReader构造函数自动打开该文件。ReadLine方法读取的每一行文本,并读取递增到下一行将文件指针。当ReadLine方法到达文件结尾时, 它将返回空引用。
1在记事本中创建示例文本文件。若要这样做,请按照下列步骤操作:
1在记事本中粘贴以下文本:
hello world
1将文件另存为Sample.txt。
2启动Microsoft Visual Studio。
3在文件菜单上指向新建,然后单击项目。
4在项目类型框中,单击Visual C# 项目,然后单击模板下的控制台应用程序
注意在Visual Studio 2005 或Visual Studio 2008 中在项目类型框中,单击Visual C#,然后单击在模板下的控制台应用程序。
5Class1.cs 文件的开头添加以下代码:
using System.IO;
6Visual Studio 2005 或Visual Studio 2008,默认的文件的注释是Program.cs。
7将下面的代码添加到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.");
}
8块。
9在调试菜单上单击编译并运行该应用程序,请开始。若要关闭控制台窗口按
ENTER。控制台窗口将显示Sample.txt 文件
Hello world
10中的内容
写文本文件(示例1)
下面的代码使用StreamWriter类打开、写入,和以关闭该文本文件。StreamReader类以类似方式您可以将文本文件的路径传递给该StreamWriter构造函数,以自动打开该文件。WriteLine方法写入文本文件的完整文本行。
11启动Visual Studio。
12在文件菜单上指向新建,然后单击项目。
13在项目类型框中,单击Visual C# 项目,然后单击模板下的控制台应用程序。
注意在Visual Studio 2005 或Visual Studio 2008 中在项目类型框中,单击Visual C#,然后单击模板下的CLR 控制台应用程序。
14Class1.cs 文件的开头添加以下代码:
using System.IO;
15将下面的代码添加到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.");
}
16块。
17在调试菜单上单击编译并运行该应用程序,请开始。此代码创建的在文本编辑器(如记事本)的驱动器c。打开Test.txt 上名为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) 字符组合。当您想要一次写入一个字符时,这是很有用。
18启动Visual Studio。
19在文件菜单上指向新建,然后单击项目。
20在项目类型框中,单击Visual C# 项目,然后单击模板下的控制台应用程序
注意在Visual Studio 2005 或Visual Studio 2008 单击Visual C#在项目类型,然后单击模板下的控制台应用程序
21Class1.cs文件的开头添加以下代码:
using System.IO;
using System.Text;
22Visual Studio 2005 或Visual Studio 2008,默认的文件的注释是Program.cs。
23将下面的代码添加到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.");
}
24在调试菜单上单击编译并运行该应用程序,请开始。此代码创建一个命名Test1.txt 为驱动器c。打开Test1.txt 在文本编辑器(如记事本)上的文件。Test1.txt 包含单行文本:
0123456789
完成代码列表
?读取文本文件
//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程序块来处理错误和异常代码。专门,您可能希望释放最后块中文件的句柄,使该文件未被无限期锁定。一些可能的错误包括一个文件不存在或已在使用中的文件。