本文介绍如何从读取和写入文本文件通过 VisualC #。
要求
以下列表概括了推荐硬件、 软件、 网络结构, 以及 ServicePack, 必须:
| • |
VisualC # .NET 或 VisualC # 2005 |
本文假定您已熟悉以下主题:
| • |
VisualC # .NET 或 VisualC # 2005 |
读取和写入文本文件
The 读取文本文件 一部分本文介绍了如何使用 StreamReader 类来读取文本文件。 The 写文本文件 (示例 1) 和 写文本文件 (示例 2) 部分描述如何使用 StreamWriter 类向文件写入文本。
读取文本文件
以下代码使用 StreamReader 类来打开、 以读取, 和关闭文本文件。 可向 StreamReader 构造函数以自动打开文件传递文本文件的路径名。 ReadLine 方法读取每一行文本, 并递增文件指针以下行如读取。 ReadLine 方法到达文件, 末尾时返回空引用。
| 1. |
在记事本中创建示例文本文件。 要这样做, 请按照下列步骤操作:
| a. |
在记事本中粘贴下列文本:
hello wo-rld |
| b. |
将文件保存为 Sample.txt。 |
|
| 2. |
启动 MicrosoftVisualStudio.NET 或 Microsoft Visual Studio 2005。 |
| 3. |
在 文件 菜单, 指向 新建 , 然后单击 项目 。 |
| 4. |
单击 项目类型 , 下 Visual C# 项目 , 然后单击 模板 下 的控制台应用程序
注意 入 Visual Studio 2005, 单击 VisualC # 项目类型 , 下和然后单击 模板 下 的控制台应用程序 。 |
| 5. |
Class 1 .cs 文件的开头添加下列代码:
using System.IO;
注意 在 Visual Studio 2005, 默认文件是 Program.cs。 |
| 6. |
以下代码添加到 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."); }
|
| 7. |
在 调试 菜单上, 单击 开始 以编译并以运行该应用程序。 按 ENTER 键关闭控制台窗口。 控制台窗口显示 Boot.ini 文件的内容。 注意, Boot.ini 文件的内容可能因到另一台计算机。 以下输出是一个示例 Boot.ini 文件: [boot loader] timeout=30 default=multi(0)disk(0)rdisk(0)partition(1)\WINNT [operating systems] multi(0)disk(0)rdisk(0)partition(1)\WINNT="Microsoft Windows 2000 Professional" /fastdetect |
写文本文件 (示例 1)
以下代码使用 StreamWriter 类来打开、 写入, 和关闭文本文件。 向 StreamWriter 构造函数以自动打开文件以类似方式对 StreamReader 类, 可传递文本文件的路径名。 WriteLine 方法写入完整本行文本文件。
| 1. |
启动 VisualStudio.NET 或 Visual Studio 2005。 |
| 2. |
在 文件 菜单, 指向 新建 , 然后单击 项目 。 |
| 3. |
单击 项目类型 , 下 VisualC # 项目 , 然后单击 模板 下 控制台应用程序 。
注意 入 Visual Studio 2005, 单击 VisualC # 项目类型 , 下和然后单击 模板 下 CLR 控制台应用程序 。 |
| 4. |
Class 1 .cs 文件的开头添加下列代码:
using System.IO; |
| 5. |
以下代码添加到 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."); } |
| 6. |
在 调试 菜单上, 单击 开始 以编译并以运行该应用程序。 此代码创建在文本编辑器如记事本打开 Test.txt C 驱动器上名为 Test.txt 文件。 Test.txt 包含两行的文本:
Hello World!! From the StreamWriter class
|
写文本文件 (示例 2)
以下代码使用 StreamWriter 类来打开、 写入, 和关闭文本文件。 与上例, 此代码将两个附加参数传递到构造函数。 第一个参数是文件的路径和文件名。 第二个参数, True , 指定打开文件中追加模式。 的文件内容如果指定为第二个参数, False 是覆盖每次运行代码。 第三个参数指定 Unicode , 以便 StreamWriter 编码 Unicode 格式文件中。 还可指定下列编码方法对三个参数:
| • |
ASC11 |
| • |
Unicode |
| • |
UTF7 |
| • |
UTF 8 |
Write 方法是类似于 WriteLine 方法, 在于 Write 方法不会自动嵌入回车或馈送行 (CR/LF) 字符组合。 如果您想要一次写入一个字符这是很有用。
| 1. |
启动 VisualStudio.NET 或 Visual Studio 2005。 |
| 2. |
在 文件 菜单, 指向 新建 , 然后单击 项目 。 |
| 3. |
单击 项目类型 , 下 Visual C# 项目 , 然后单击 模板 下 的控制台应用程序
注意 在 Visual Studio 2005, VisualC # 依次 模板 下 ProjectTypes@@ , 下 控制台应用程序 |
| 4. |
Class 1 .cs 文件的开头添加下列代码:
using System.IO;using System.Text;
注意 在 Visual Studio 2005, 默认文件是 Program.cs。 |
| 5. |
以下代码添加到 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."); } |
| 6. |
在 调试 菜单上, 单击 开始 以编译并以运行该应用程序。 此代码创建名为 Test1.txt 在文本编辑器如记事本打开 Test1.txt C 驱动器上文件。 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:\\boot.ini");
//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."); } } } } |
疑难解答
对于所有文件操作, 它是良好编程做法来包装内 try-catch - finally 块来处理错误和异常代码。 特别, 可能要释放手柄, 最后块中文件以便文件不是无限期锁定。 一些可能错误包括文件不存在或已在使用文件。