Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created July 10, 2015 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fhernd/457ffa746e44ef3613ce to your computer and use it in GitHub Desktop.
Save Fhernd/457ffa746e44ef3613ce to your computer and use it in GitHub Desktop.
Demostración de escritura y lectura de archivos de texto plano en C#.
// OrtizOL - xCSw - http://ortizol.blogspot.com
using System;
using System.IO;
using System.Text;
namespace Receta.CSharp.R0507
{
public class LecturaEscrituraArchivo
{
public static void Main()
{
Console.WriteLine(Environment.NewLine);
// Creación de archivo de texto plano:
using (FileStream fs = new FileStream("ArchivoTexto.txt", FileMode.Create))
{
// Creación de objeto StreamWriter para la escritura
// sobre el archivo recién creado. El sistema de
// codificación es UTF-8:
using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8))
{
// Escritura de diferentes tipos de datos:
sw.WriteLine(987.65M);
sw.WriteLine("OrtizOL - xCSw");
sw.WriteLine('!');
}
}
Console.WriteLine("Para continuar con la lectura presione Enter.");
Console.ReadLine();
// Apertura del archivo:
using (FileStream fs = new FileStream("ArchivoTexto.txt", FileMode.Open))
{
using(StreamReader sr = new StreamReader(fs, Encoding.UTF8))
{
Console.WriteLine(Decimal.Parse(sr.ReadLine()));
Console.WriteLine(sr.ReadLine());
Console.WriteLine(Char.Parse(sr.ReadLine()));
}
}
Console.WriteLine(Environment.NewLine);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment