Console.Read 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
從標準輸入資料流讀取下一個字元。
public:
static int Read();
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static int Read ();
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
public static int Read ();
public static int Read ();
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
static member Read : unit -> int
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("android")>]
static member Read : unit -> int
static member Read : unit -> int
Public Shared Function Read () As Integer
傳回
輸入資料流的下一個字元,或為 -1 (如果目前不再有字元可以讀取)。
- 屬性
例外狀況
發生 I/O 錯誤。
範例
下列範例示範 Read 方法。
// This example demonstrates the Console.Read() method.
using namespace System;
int main()
{
String^ m1 = "\nType a string of text then press Enter. "
"Type '+' anywhere in the text to quit:\n";
String^ m2 = "Character '{0}' is hexadecimal 0x{1:x4}.";
String^ m3 = "Character is hexadecimal 0x{0:x4}.";
Char ch;
int x;
//
Console::WriteLine( m1 );
do
{
x = Console::Read();
try
{
ch = Convert::ToChar( x );
if ( Char::IsWhiteSpace( ch ) )
{
Console::WriteLine( m3, x );
if ( ch == 0x0a )
Console::WriteLine( m1 );
}
else
Console::WriteLine( m2, ch, x );
}
catch ( OverflowException^ e )
{
Console::WriteLine( "{0} Value read = {1}.", e->Message, x );
ch = Char::MinValue;
Console::WriteLine( m1 );
}
}
while ( ch != '+' );
}
/*
This example produces the following results:
Type a string of text then press Enter. Type '+' anywhere in the text to quit:
The quick brown fox.
Character 'T' is hexadecimal 0x0054.
Character 'h' is hexadecimal 0x0068.
Character 'e' is hexadecimal 0x0065.
Character is hexadecimal 0x0020.
Character 'q' is hexadecimal 0x0071.
Character 'u' is hexadecimal 0x0075.
Character 'i' is hexadecimal 0x0069.
Character 'c' is hexadecimal 0x0063.
Character 'k' is hexadecimal 0x006b.
Character is hexadecimal 0x0020.
Character 'b' is hexadecimal 0x0062.
Character 'r' is hexadecimal 0x0072.
Character 'o' is hexadecimal 0x006f.
Character 'w' is hexadecimal 0x0077.
Character 'n' is hexadecimal 0x006e.
Character is hexadecimal 0x0020.
Character 'f' is hexadecimal 0x0066.
Character 'o' is hexadecimal 0x006f.
Character 'x' is hexadecimal 0x0078.
Character '.' is hexadecimal 0x002e.
Character is hexadecimal 0x000d.
Character is hexadecimal 0x000a.
Type a string of text then press Enter. Type '+' anywhere in the text to quit:
^Z
Value was either too large or too small for a character. Value read = -1.
Type a string of text then press Enter. Type '+' anywhere in the text to quit:
+
Character '+' is hexadecimal 0x002b.
*/
// This example demonstrates the Console.Read() method.
using System;
class Sample
{
public static void Main()
{
string m1 = "\nType a string of text then press Enter. " +
"Type '+' anywhere in the text to quit:\n";
string m2 = "Character '{0}' is hexadecimal 0x{1:x4}.";
string m3 = "Character is hexadecimal 0x{0:x4}.";
char ch;
int x;
//
Console.WriteLine(m1);
do
{
x = Console.Read();
try
{
ch = Convert.ToChar(x);
if (Char.IsWhiteSpace(ch))
{
Console.WriteLine(m3, x);
if (ch == 0x0a)
Console.WriteLine(m1);
}
else
{
Console.WriteLine(m2, ch, x);
}
}
catch (OverflowException e)
{
Console.WriteLine("{0} Value read = {1}.", e.Message, x);
ch = Char.MinValue;
Console.WriteLine(m1);
}
} while (ch != '+');
}
}
/*
This example produces the following results:
Type a string of text then press Enter. Type '+' anywhere in the text to quit:
The quick brown fox.
Character 'T' is hexadecimal 0x0054.
Character 'h' is hexadecimal 0x0068.
Character 'e' is hexadecimal 0x0065.
Character is hexadecimal 0x0020.
Character 'q' is hexadecimal 0x0071.
Character 'u' is hexadecimal 0x0075.
Character 'i' is hexadecimal 0x0069.
Character 'c' is hexadecimal 0x0063.
Character 'k' is hexadecimal 0x006b.
Character is hexadecimal 0x0020.
Character 'b' is hexadecimal 0x0062.
Character 'r' is hexadecimal 0x0072.
Character 'o' is hexadecimal 0x006f.
Character 'w' is hexadecimal 0x0077.
Character 'n' is hexadecimal 0x006e.
Character is hexadecimal 0x0020.
Character 'f' is hexadecimal 0x0066.
Character 'o' is hexadecimal 0x006f.
Character 'x' is hexadecimal 0x0078.
Character '.' is hexadecimal 0x002e.
Character is hexadecimal 0x000d.
Character is hexadecimal 0x000a.
Type a string of text then press Enter. Type '+' anywhere in the text to quit:
^Z
Value was either too large or too small for a character. Value read = -1.
Type a string of text then press Enter. Type '+' anywhere in the text to quit:
+
Character '+' is hexadecimal 0x002b.
*/
// This example demonstrates the Console.Read() method.
open System
// string m2 = "Character '{0}' is hexadecimal 0x{1:x4}.";
// string m3 = "Character is hexadecimal 0x{0:x4}.";
// char ch;
printfn "\nType a string of text then press Enter. Type '+' anywhere in the text to quit:\n"
let mutable ch = ' '
let mutable x = 0
while ch <> '+' do
x <- Console.Read()
try
ch <- Convert.ToChar x
if Char.IsWhiteSpace ch then
printfn $"Character is hexadecimal 0x{x:x4}."
if ch = char '\u000A' then
printfn "\nType a string of text then press Enter. Type '+' anywhere in the text to quit:\n"
else
printfn $"Character '{ch}' is hexadecimal 0x{x:x4}."
with :? OverflowException as e ->
printfn $"{e.Message} Value read = {x}."
ch <- Char.MinValue
printfn "\nType a string of text then press Enter. Type '+' anywhere in the text to quit:\n"
// This example produces the following results:
//
// Type a string of text then press Enter. Type '+' anywhere in the text to quit:
//
// The quick brown fox.
// Character 'T' is hexadecimal 0x0054.
// Character 'h' is hexadecimal 0x0068.
// Character 'e' is hexadecimal 0x0065.
// Character is hexadecimal 0x0020.
// Character 'q' is hexadecimal 0x0071.
// Character 'u' is hexadecimal 0x0075.
// Character 'i' is hexadecimal 0x0069.
// Character 'c' is hexadecimal 0x0063.
// Character 'k' is hexadecimal 0x006b.
// Character is hexadecimal 0x0020.
// Character 'b' is hexadecimal 0x0062.
// Character 'r' is hexadecimal 0x0072.
// Character 'o' is hexadecimal 0x006f.
// Character 'w' is hexadecimal 0x0077.
// Character 'n' is hexadecimal 0x006e.
// Character is hexadecimal 0x0020.
// Character 'f' is hexadecimal 0x0066.
// Character 'o' is hexadecimal 0x006f.
// Character 'x' is hexadecimal 0x0078.
// Character '.' is hexadecimal 0x002e.
// Character is hexadecimal 0x000d.
// Character is hexadecimal 0x000a.
//
// Type a string of text then press Enter. Type '+' anywhere in the text to quit:
//
// ^Z
// Value was either too large or too small for a character. Value read = -1.
//
// Type a string of text then press Enter. Type '+' anywhere in the text to quit:
//
// +
// Character '+' is hexadecimal 0x002b.
' This example demonstrates the Console.Read() method.
Class Sample
Public Shared Sub Main()
Dim m1 As String = _
vbCrLf & _
"Type a string of text then press Enter. " & _
"Type '+' anywhere in the text to quit:" & _
vbCrLf
Dim m2 As String = "Character '{0}' is hexadecimal 0x{1:x4}."
Dim m3 As String = "Character is hexadecimal 0x{0:x4}."
Dim ch As Char
Dim x As Integer
'
Console.WriteLine(m1)
Do
x = Console.Read()
Try
ch = Convert.ToChar(x)
If Char.IsWhiteSpace(ch) Then
Console.WriteLine(m3, x)
If ch = vbLf Then
Console.WriteLine(m1)
End If
Else
Console.WriteLine(m2, ch, x)
End If
Catch e As OverflowException
Console.WriteLine("{0} Value read = {1}.", e.Message, x)
ch = Char.MinValue
Console.WriteLine(m1)
End Try
Loop While ch <> "+"c
End Sub
End Class
'
'This example produces the following results:
'
'Type a string of text then press Enter. Type '+' anywhere in the text to quit:
'
'The quick brown fox.
'Character 'T' is hexadecimal 0x0054.
'Character 'h' is hexadecimal 0x0068.
'Character 'e' is hexadecimal 0x0065.
'Character is hexadecimal 0x0020.
'Character 'q' is hexadecimal 0x0071.
'Character 'u' is hexadecimal 0x0075.
'Character 'i' is hexadecimal 0x0069.
'Character 'c' is hexadecimal 0x0063.
'Character 'k' is hexadecimal 0x006b.
'Character is hexadecimal 0x0020.
'Character 'b' is hexadecimal 0x0062.
'Character 'r' is hexadecimal 0x0072.
'Character 'o' is hexadecimal 0x006f.
'Character 'w' is hexadecimal 0x0077.
'Character 'n' is hexadecimal 0x006e.
'Character is hexadecimal 0x0020.
'Character 'f' is hexadecimal 0x0066.
'Character 'o' is hexadecimal 0x006f.
'Character 'x' is hexadecimal 0x0078.
'Character '.' is hexadecimal 0x002e.
'Character is hexadecimal 0x000d.
'Character is hexadecimal 0x000a.
'
'Type a string of text then press Enter. Type '+' anywhere in the text to quit:
'
'^Z
'Value was either too large or too small for a character. Value read = -1.
'
'Type a string of text then press Enter. Type '+' anywhere in the text to quit:
'
'+
'Character '+' is hexadecimal 0x002b.
'
備註
當您輸入輸入字元時,方法 Read 會封鎖其傳回;當您按下 Enter 按鍵時就會終止。 例如,按 Enter 會將平臺相依的行終止序列附加至您的輸入 (,Windows 會附加歸位字元換行字元序列) 。 對 方法的後續呼叫 Read 會一次擷取您的輸入一個字元。 擷取最後一個字元之後, Read 再次封鎖其傳回,並重複迴圈。
重要
方法 ReadLine 或 KeyAvailable 屬性和 ReadKey 方法最好是使用 Read 方法。
請注意,除非您執行下列其中一個動作,否則方法不會傳回 -1:
同步選取 Control 修飾詞鍵和 Z 主控台鍵 (Ctrl+Z) ,以發出檔案結尾條件的訊號。 如果您是在 Windows 上,您也必須按 Enter 主控台鍵。
按下對等按鍵,以發出檔案結尾條件的訊號,例如 Windows 中的 F6 函式按鍵。
將輸入資料流程重新導向至具有實際檔案結尾字元的來源,例如文字檔。