您好,登錄后才能下訂單哦!
VB.NET中 ReadLine()方法如何使用,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
修改源代碼
更改 C# 源文件 (class1.cs),如下面以斜體突出顯示的代碼所示。其他的差異(如類名)可忽略不計。
// Import namespaces using System; using System.Collections; using System.IO; // Declare namespace namespace MsdnAA { // Declare application class class QuickSortApp { // Application initialization static void Main (string[] szArgs) { ... ... ... // Read contents of source file string szSrcLine; ArrayList szContents = new ArrayList (); FileStream fsInput = new FileStream (szSrcFile, FileMode.Open, FileAccess.Read); StreamReader srInput = new StreamReader (fsInput); while ((szSrcLine = srInput.ReadLine ()) != null) { // Append to array szContents.Add (szSrcLine); } srInput.Close (); fsInput.Close (); // TODO: Pass to QuickSort function // Write sorted lines FileStream fsOutput = new FileStream (szDestFile, FileMode.Create, FileAccess.Write); StreamWriter srOutput = new StreamWriter (fsOutput); for (int nIndex = 0; nIndex < szContents.Count; nIndex++) { // Write line to output file srOutput.WriteLine (szContents[nIndex]); } srOutput.Close (); fsOutput.Close (); // Report program success Console.WriteLine ("\nThe sorted lines have been written.\n\n"); } } }
從源文件進行讀取
使用 FileStream 類打開源文件,然后加入 StreamReader 類,這樣我們就可以使用它的VB.NET ReadLine()方法了。現(xiàn)在,我們調(diào)用VB.NET ReadLine()方法,直到它返回 null,這表示到達文件結(jié)尾。在循環(huán)過程中,我們將讀取的行存儲到字符串數(shù)組中,然后關(guān)閉這兩個對象。
寫入輸出文件
假設(shè)已經(jīng)用 QuickSort 對字符串數(shù)組進行了排序,接下來要做的事情就是輸出數(shù)組的內(nèi)容。按照同樣的方式,我們將 StreamWriter 對象附加到 FileStream 對象上。這使得我們可以使用 WriteLine() 方法,該方法能夠很方便地模仿 Console 類的行為。一旦遍歷了數(shù)組,我們便可以象前面一樣關(guān)閉這兩個對象。
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。