Console.ReadLine (Método)
Ensamblado: mscorlib (en mscorlib.dll)
| Tipo de excepción | Condición |
|---|---|
| Error de E/S. |
|
| No hay memoria suficiente como para asignar un búfer a la cadena devuelta. |
|
| El número de caracteres de la siguiente línea de caracteres es mayor que Int32.MaxValue |
Una línea se define como una secuencia de caracteres seguida de un retorno de carro (hexadecimal 0x000d), un salto de línea (hexadecimal 0x000a) o el valor de la propiedad Environment.NewLine. La cadena devuelta no contiene el carácter o los caracteres de terminación.
Si este método produce OutOfMemoryException, la posición del lector en la secuencia Stream subyacente avanza el número de caracteres que el método haya podido leer, aunque los caracteres ya leídos en el búfer ReadLine interno se descartan. Dado que no se puede modificar la posición del lector en la secuencia, no es posible recuperar los caracteres ya leídos y sólo se puede obtener acceso a ellos reinicializando el objeto TextReader. Si no se conoce la posición inicial dentro de la secuencia o la secuencia no admite operaciones de búsqueda, es necesario reinicializar también la secuencia Stream subyacente.
Para evitar que se produzca una situación de este tipo y generar código robusto, debe utilizar el método Read y almacenar los caracteres de lectura en un búfer preasignado.
En el ejemplo de código siguiente se ilustra el uso del método ReadLine.
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; } }
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, Windows Mobile para Pocket PC, Windows Mobile para Smartphone, Windows Server 2003, Windows XP Media Center, Windows XP Professional x64, Windows XP SP2, Windows XP Starter Edition
.NET Framework no admite todas las versiones de cada plataforma. Para obtener una lista de las versiones admitidas, vea Requisitos del sistema.