Console.ReadLine 方法
从标准输入流读取下一行字符。
**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
Public Shared Function ReadLine As String
用法
Dim returnValue As String
returnValue = Console.ReadLine
public static string ReadLine ()
public:
static String^ ReadLine ()
public static String ReadLine ()
public static function ReadLine () : String
返回值
输入流中的下一行字符;如果没有更多的可用行,则为 空引用(在 Visual Basic 中为 Nothing)。
异常
异常类型 | 条件 |
---|---|
发生了 I/O 错误。 |
|
内存不足,无法为返回的字符串分配缓冲区。 |
|
下一行字符中的字符数大于 Int32.MaxValue。 |
备注
行被定义为后跟回车符(十六进制 0x000d)、换行符(十六进制 0x000a)或 Environment.NewLine 属性值的字符序列。返回的字符串不包含终止字符。
如果此方法引发 OutOfMemoryException,则读取器在基础 Stream 的位置会前移若干字符,前移的字符数就是该方法能够读取的字符数,但是会放弃已经读入内部 ReadLine 缓冲区的字符。因为无法更改读取器在流中的位置,所以已经读取的字符是无法恢复的,只能通过重新初始化 TextReader 访问它们。如果流内的初始位置未知,或者流不支持查找,则也需要重新初始化基础 Stream。
为避免这种情况并产生可靠的代码,应使用 Read 方法,并将已读取的字符存储在预先分配的缓冲区内。
示例
下面的代码示例演示 ReadLine 方法的用法。
Public Class InsertTabs
Private Const tabSize As Integer = 4
Private Const usageText As String = "Usage: INSERTTABS inputfile.txt outputfile.txt"
'Entry point which delegates to C-style main Private Function
Public Overloads Shared Sub Main()
System.Environment.ExitCode = Main(System.Environment.GetCommandLineArgs())
End Sub
Overloads Public Shared Function Main(args() As String) As Integer
Dim writer As StreamWriter = Nothing
If args.Length < 3 Then
Console.WriteLine(usageText)
Return 1
End If
Try
writer = New StreamWriter(args(2))
Console.SetOut(writer)
Console.SetIn(New StreamReader(args(1)))
Catch e As IOException
Dim errorWriter As TextWriter = Console.Error
errorWriter.WriteLine(e.Message)
errorWriter.WriteLine(usageText)
Return 1
End Try
Dim line As String
line = Console.ReadLine()
While Not line Is Nothing
Dim newLine As String = line.Replace("".PadRight(tabSize, " "c), ControlChars.Tab)
Console.WriteLine(newLine)
line = Console.ReadLine()
End While
writer.Close()
' Recover the standard output stream so that a
' completion message can be displayed.
Dim standardOutput As New StreamWriter(Console.OpenStandardOutput())
standardOutput.AutoFlush = True
Console.SetOut(standardOutput)
Console.WriteLine("INSERTTABS has completed the processing of {0}.", args(0))
Return 0
End Function 'Main
End Class 'InsertTabs
public class InsertTabs {
private const int tabSize = 4;
private const string usageText = "Usage: INSERTTABS inputfile.txt outputfile.txt";
public static int Main(string[] args) {
StreamWriter writer = null;
if (args.Length < 2) {
Console.WriteLine(usageText);
return 1;
}
try {
writer = new StreamWriter(args[1]);
Console.SetOut(writer);
Console.SetIn(new StreamReader(args[0]));
}
catch(IOException e) {
TextWriter errorWriter = Console.Error;
errorWriter.WriteLine(e.Message);
errorWriter.WriteLine(usageText);
return 1;
}
string line;
while ((line = Console.ReadLine()) != null) {
string newLine = line.Replace(("").PadRight(tabSize, ' '), "\t");
Console.WriteLine(newLine);
}
writer.Close();
// Recover the standard output stream so that a
// completion message can be displayed.
StreamWriter standardOutput = new StreamWriter(Console.OpenStandardOutput());
standardOutput.AutoFlush = true;
Console.SetOut(standardOutput);
Console.WriteLine("INSERTTABS has completed the processing of {0}.", args[0]);
return 0;
}
}
int main()
{
array<String^>^args = Environment::GetCommandLineArgs();
const int tabSize = 4;
String^ usageText = "Usage: INSERTTABS inputfile.txt outputfile.txt";
StreamWriter^ writer = nullptr;
if ( args->Length < 3 )
{
Console::WriteLine( usageText );
return 1;
}
try
{
writer = gcnew StreamWriter( args[ 2 ] );
Console::SetOut( writer );
Console::SetIn( gcnew StreamReader( args[ 1 ] ) );
}
catch ( IOException^ e )
{
TextWriter^ errorWriter = Console::Error;
errorWriter->WriteLine( e->Message );
errorWriter->WriteLine( usageText );
return 1;
}
String^ line;
while ( (line = Console::ReadLine()) != nullptr )
{
String^ newLine = line->Replace( ((String^)"")->PadRight( tabSize, ' ' ), "\t" );
Console::WriteLine( newLine );
}
writer->Close();
// Recover the standard output stream so that a
// completion message can be displayed.
StreamWriter^ standardOutput = gcnew StreamWriter( Console::OpenStandardOutput() );
standardOutput->AutoFlush = true;
Console::SetOut( standardOutput );
Console::WriteLine( "INSERTTABS has completed the processing of {0}.", args[ 1 ] );
return 0;
}
public class InsertTabs
{
private static int tabSize = 4;
private static String usageText = "Usage: INSERTTABS inputfile.txt"
+ " outputfile.txt";
public static void main(String[] args)
{
StreamWriter writer = null;
if (args.length < 2) {
Console.WriteLine(usageText);
return ;
}
try {
writer = new StreamWriter(args[1]);
Console.SetOut(writer);
Console.SetIn(new StreamReader(args[0]));
}
catch (IOException e) {
TextWriter errorWriter = Console.get_Error();
errorWriter.WriteLine(e.get_Message());
errorWriter.WriteLine(usageText);
return ;
}
String line;
while (((line = Console.ReadLine()) != null)) {
String newLine = line.Replace("".PadRight(tabSize, ' '), "\t");
Console.WriteLine(newLine);
}
writer.Close();
// Recover the standard output stream so that a
// completion message can be displayed.
StreamWriter standardOutput = new StreamWriter(Console.
OpenStandardOutput());
standardOutput.set_AutoFlush(true);
Console.SetOut(standardOutput);
Console.WriteLine("INSERTTABS has completed the processing of {0}.",
args[0]);
} //main
} //InsertTabs
const tabSize = 4;
const usageText = "Usage: INSERTTABS inputfile.txt outputfile.txt";
var writer : StreamWriter = null;
var args = Environment.GetCommandLineArgs();
if (args.Length != 3) {
Console.WriteLine(usageText);
Environment.Exit(1);
}
try {
writer = new StreamWriter(args[2]);
Console.SetOut(writer);
Console.SetIn(new StreamReader(args[1]));
}
catch(e : IOException) {
var errorWriter = Console.Error;
errorWriter.WriteLine(e.Message);
errorWriter.WriteLine(usageText);
Environment.Exit(1);
}
var line;
while ((line = Console.ReadLine()) != null) {
var newLine = line.Replace(("").PadRight(tabSize, ' '), "\t");
Console.WriteLine(newLine);
}
writer.Close();
// Recover the standard output stream so that a
// completion message can be displayed.
var standardOutput = new StreamWriter(Console.OpenStandardOutput());
standardOutput.AutoFlush = true;
Console.SetOut(standardOutput);
Console.WriteLine("INSERTTABS has completed the processing of {0}.", args[0]);
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0