StringWriter (Clase)
Ensamblado: mscorlib (en mscorlib.dll)
En la siguiente tabla se muestran ejemplos de otras tareas de E/S típicas o relacionadas.
| Para realizar esta operación... | Vea el ejemplo de este tema... |
|---|---|
| Crear un archivo de texto | |
| Escribir en un archivo de texto | |
| Leer de un archivo de texto | |
| Anexar texto a un archivo | |
| Obtener el tamaño de un archivo | |
| Obtener los atributos de un archivo | |
| Establecer los atributos de un archivo | |
| Determinar si un archivo existe | |
| Leer de un archivo binario | |
| Escribir en un archivo binario |
En el ejemplo de código siguiente se muestra la creación de un párrafo continuo a partir de un grupo de frases a doble espacio y la posterior conversión del párrafo al texto original.
using System; using System.IO; class StringRW { static void Main() { string textReaderText = "TextReader is the abstract base " + "class of StreamReader and StringReader, which read " + "characters from streams and strings, respectively.\n\n" + "Create an instance of TextReader to open a text file " + "for reading a specified range of characters, or to " + "create a reader based on an existing stream.\n\n" + "You can also use an instance of TextReader to read " + "text from a custom backing store using the same " + "APIs you would use for a string or a stream.\n\n"; Console.WriteLine("Original text:\n\n{0}", textReaderText); // 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); // Re-create textReaderText from aParagraph. int intCharacter; char convertedCharacter; StringWriter strWriter = new StringWriter(); strReader = new StringReader(aParagraph); while(true) { intCharacter = strReader.Read(); // Check for the end of the string // before converting to a character. if(intCharacter == -1) break; convertedCharacter = Convert.ToChar(intCharacter); if(convertedCharacter == '.') { strWriter.Write(".\n\n"); // Bypass the spaces between sentences. strReader.Read(); strReader.Read(); } else { strWriter.Write(convertedCharacter); } } Console.WriteLine("\nOriginal text:\n\n{0}", strWriter.ToString()); } }
import System.*;
import System.IO.*;
class StringRW
{
public static void main(String[] args)
{
String textReaderText = "TextReader is the abstract base "
+ "class of StreamReader and StringReader, which read "
+ "characters from streams and strings, respectively.\n\n"
+ "Create an instance of TextReader to open a text file "
+ "for reading a specified range of characters, or to "
+ "create a reader based on an existing stream.\n\n"
+ "You can also use an instance of TextReader to read "
+ "text from a custom backing store using the same "
+ "APIs you would use for a string or a stream.\n\n";
Console.WriteLine("Original text:\n\n{0}", textReaderText);
// From textReaderText, create a continuous paragraph
// with two spaces between each sentence.
String aParagraph = "";
String aLine;
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);
// Re-create textReaderText from aParagraph.
int intCharacter;
char convertedCharacter;
StringWriter strWriter = new StringWriter();
strReader = new StringReader(aParagraph);
while (true) {
intCharacter = strReader.Read();
// Check for the end of the string
// before converting to a character.
if (intCharacter == -1) {
break ;
}
convertedCharacter = Convert.ToChar(intCharacter);
if (convertedCharacter == '.') {
strWriter.Write(".\n\n");
// Bypass the spaces between sentences.
strReader.Read();
strReader.Read();
}
else {
strWriter.Write(convertedCharacter);
}
}
Console.WriteLine("\nOriginal text:\n\n{0}", strWriter.ToString());
} //main
} //StringRW
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.