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 函数键。
将输入流重定向到具有实际文件尾字符的源(如文本文件)。