如何掌握强大的VB.NET ReadLine()方法

开发 后端
这里介绍VB.NET ReadLine()方法,使用 FileStream 类打开源文件,然后加入 StreamReader 类,这样我们就可以使用它的VB.NET ReadLine()方法了。

经过长时间学习VB.NET ReadLine()方法,于是和大家分享一下,看完本文你肯定有不少收获,希望本文能教会你更多东西。

现在,让我们来实现读取输入文件和写入输出文件。我们将每一行读取到一个字符串数组中,然后输出该字符串数组。在下一步中,我们将使用 QuickSort 算法来对该数组进行排序。

修改源代码

更改 C# 源文件 (class1.cs),如下面以斜体突出显示的代码所示。其他的差异(如类名)可忽略不计。

  1. // Import namespaces  
  2. using System;  
  3. using System.Collections;  
  4. using System.IO;  
  5. // Declare namespace  
  6. namespace MsdnAA  
  7. {  
  8.     // Declare application class  
  9.     class QuickSortApp  
  10.     {  
  11.         // Application initialization  
  12.         static void Main (string[] szArgs)  
  13.         {  
  14.             ... ... ...  
  15.             // Read contents of source file  
  16.             string szSrcLine;  
  17.             ArrayList szContents = new ArrayList ();  
  18.             FileStream fsInput = new FileStream (szSrcFile, FileMode.Open,  
  19.                 FileAccess.Read);  
  20.             StreamReader srInput = new StreamReader (fsInput);  
  21.             while ((szSrcLine = srInput.ReadLine ()) != null)  
  22.             {  
  23.                 // Append to array  
  24.                 szContents.Add (szSrcLine);  
  25.             }  
  26.             srInput.Close ();  
  27.             fsInput.Close ();  
  28.             // TODO: Pass to QuickSort function  
  29.             // Write sorted lines  
  30.             FileStream fsOutput = new FileStream (szDestFile,  
  31.                 FileMode.Create, FileAccess.Write);  
  32.             StreamWriter srOutput = new StreamWriter (fsOutput);  
  33.             for (int nIndex = 0; nIndex < szContents.Count; nIndex++)  
  34.             {  
  35.                 // Write line to output file  
  36.                 srOutput.WriteLine (szContents[nIndex]);  
  37.             }  
  38.             srOutput.Close ();  
  39.             fsOutput.Close ();  
  40.             // Report program success  
  41.             Console.WriteLine ("\nThe sorted lines have been written.\n\n");  
  42.         }  
  43.     }  
  44. }  

从源文件进行读取

使用 FileStream 类打开源文件,然后加入 StreamReader 类,这样我们就可以使用它的VB.NET ReadLine()方法了。现在,我们调用VB.NET ReadLine()方法,直到它返回 null,这表示到达文件结尾。在循环过程中,我们将读取的行存储到字符串数组中,然后关闭这两个对象。

调用VB.NET ReadLine()方法


写入输出文件

假设已经用 QuickSort 对字符串数组进行了排序,接下来要做的事情就是输出数组的内容。按照同样的方式,我们将 StreamWriter 对象附加到 FileStream 对象上。这使得我们可以使用 WriteLine() 方法,该方法能够很方便地模仿 Console 类的行为。一旦遍历了数组,我们便可以象前面一样关闭这两个对象。

使用 WriteLine()方法

【编辑推荐】

  1. 全面展示VB.NET服务器端
  2. 浅谈VB.NET数组声明和初始化
  3. 描述VB.NET fnSimpleObjectToXML
  4. 讨论VB.NET使用Sorted Lists
  5. 讲解VB.NET COMBOBOX控件
责任编辑:佚名 来源: IT168
相关推荐

2010-01-13 17:47:59

VB.NET拖放

2009-10-27 14:50:25

VB.NET控件数组

2010-01-11 15:54:48

VB.NET操作缩放图

2010-01-21 10:35:17

VB.NET查询包含

2009-10-29 09:06:26

VB.NET Web

2010-01-11 13:33:07

VB.NET使用数组

2010-01-08 18:16:52

VB.NET变量

2010-01-12 10:19:02

VB.NET操作GDI

2010-01-07 11:07:20

VB.NET读取INI

2010-01-11 14:28:14

VB.NET操作Exc

2009-10-29 13:38:05

VB.NET Shar

2010-01-14 13:59:01

2009-11-03 09:26:13

VB.NET方法

2009-11-10 11:04:09

VB.NET数据类型

2010-01-14 10:07:08

VB.NET文件名排序

2010-01-18 19:36:52

VB.NET调整控件

2009-11-10 13:43:28

VB.NET Comm

2011-05-20 16:34:35

VB.NET

2009-10-27 11:32:42

VB.NET Disp

2010-01-21 10:48:18

VB.NET扩展方法
点赞
收藏

51CTO技术栈公众号