StringReader.ReadLine 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
從目前字串讀取一行字元,並以字串形式回傳資料。
public:
override System::String ^ ReadLine();
public override string ReadLine();
public override string? ReadLine();
override this.ReadLine : unit -> string
Public Overrides Function ReadLine () As String
傳回
當字串的下一行,或 null 是當字串的末端被到達時。
例外狀況
目前的閱讀器已關閉。
記憶體不足以為回傳字串分配緩衝區。
範例
此程式碼範例是本類別更大範例 StringReader 的一部分。
// From textReaderText, create a continuous paragraph
// with two spaces between each sentence.
string aLine, aParagraph = null;
StringReader strReader = new StringReader(textReaderText);
while(true)
{
aLine = strReader.ReadLine();
if(aLine != null)
{
aParagraph = aParagraph + aLine + " ";
}
else
{
aParagraph = aParagraph + "\n";
break;
}
}
Console.WriteLine("Modified text:\n\n{0}", aParagraph);
' From textReaderText, create a continuous paragraph
' with two spaces between each sentence.
Dim aLine, aParagraph As String
Dim strReader As New StringReader(textReaderText)
While True
aLine = strReader.ReadLine()
If aLine Is Nothing Then
aParagraph = aParagraph & vbCrLf
Exit While
Else
aParagraph = aParagraph & aLine & " "
End If
End While
Console.WriteLine("Modified text:" & vbCrLf & vbCrLf & _
aParagraph)
備註
此方法會覆蓋該方法。TextReader.ReadLine
一行定義為一連串字元,接著換行(「\n」)、回車(「\r」)、回車接換行(「\r\n」)或串流結束標記。 回傳的字串不包含終止的回車或換行。 回傳值是 null 當已到達串流結束標記時。 換言之,若最後一行讀取與串流結束標記之間沒有其他資料,該方法回傳 null。
如果字串以換行序列結尾,則不會回傳額外的空行。 例如,字串"line1\nline2\n"產生的兩條線("line1""line2"和)與字串"line1\nline2"相同。
如果目前的方法拋出 , OutOfMemoryException讀取器在底層字串中的位置會依該方法能讀取的字元數前進,但已讀入內部 ReadLine 緩衝區的字元會被丟棄。 由於讀取器在字串中的位置無法更改,已讀取的字元無法恢復,只能透過重新初始化 來 StringReader存取。 為避免這種情況,請使用該 Read 方法並將讀取字元儲存在預置的緩衝區中。
下表列出其他典型或相關的 I/O 任務範例。
| 若要這麼做... | 請參閱本主題中的範例... |
|---|---|
| 建立文字檔。 | 如何:將文字寫入檔案 |
| 寫入文字檔。 | 如何:將文字寫入檔案 |
| 從文字檔讀取。 | 如何:從檔案讀取文字 |
| 在檔案中附加文字。 |
如何:開啟並附加至記錄檔 File.AppendText FileInfo.AppendText |
| 拿出檔案大小。 | FileInfo.Length |
| 取得檔案的屬性。 | File.GetAttributes |
| 設定檔案的屬性。 | File.SetAttributes |
| 判斷是否有檔案存在。 | File.Exists |
| 從二進位檔案讀取。 | 如何:讀取和寫入新建立的數據檔 |
| 寫入二進位檔案。 | 如何:讀取和寫入新建立的數據檔 |