Aprende Java Aprende Php Aprende C++ Aprende HTML 5 Aprende JavaScript Aprende JSON Aprende MySQL Aprende SQLServer Aprende Visual Basic 6 Aprende PostgreSQL Aprende SQLite Aprende Redis Aprende Kotlin Aprende XML Aprende Linux VSC Aprende Wordpress Aprende Laravel Aprende VueJS Aprende JQuery Aprende Bootstrap Aprende Netbeans Aprende Android
Sigueme en Facebook Sigueme en Twitter Sigueme en Instagram Sigueme en Youtube Sigueme en TikTok Sigueme en Whatsapp
Home / C Sharp / Visual Studio / Leer y escribir en un archivo binario

Leer y escribir en un archivo binario

Por jc mouse martes, septiembre 2, 2014

Problema: Desarrolle un programa en consola con c# para escribir y leer información de un archivo binario.

Los datos a escribir serán un nombre de tipo string, edad de tipo entero y correo electrónico de tipo string.

Solución

01 using System;
02 using System.Collections.Generic;
03 using System.Linq;
04 using System.Text;
05 using System.IO;
06 
07 namespace conDAT
08 {
09     class Program
10     {
11         static void Main(string[] args)
12         {
13 
14             guardarArchivoBinario("Olga Dísima Deloyo", 56, "olga_disima_deloyo557@gmail.com");
15 
16             leerArchivoBinario();
17         }
18 
19         static void guardarArchivoBinario( string nombre, int edad, string mail) 
20         {
21             try 
22             {
23                 //Inicializamos una nueva instancia de la clase BinaryWriter que escribe en una secuencia
24                 BinaryWriter binaryWriter = new BinaryWriter(File.Open("e:\\persona.dat", FileMode.Create));
25                 //Se escribe en el archivo
26                 binaryWriter.Write(nombre);
27                 binaryWriter.Write(edad);
28                 binaryWriter.Write(mail);
29                 binaryWriter.Close();//se cierra archivo
30             }            
31             catch (Exception ex)
32             {
33                 Console.WriteLine("Error:" +ex.Message );
34             }
35         }
36 
37         static void leerArchivoBinario()
38         {
39             try
40             {
41                 BinaryReader binaryReader = new BinaryReader(File.Open("e:\\persona.dat", FileMode.Open));
42                 //se leen los datos en el orden en los que se guardaron y se imprimen en pantalla 
43                 imprimir( binaryReader.ReadString() , binaryReader.ReadInt32() , binaryReader.ReadString() );                
44                 binaryReader.Close();//se cierra el archivo
45             }
46             catch (Exception ex)
47             {
48                 Console.WriteLine("Error:" +ex.Message );
49                 Console.ReadKey();
50             }            
51         }
52 
53         static void imprimir(string nombre, int edad, string mail) 
54         {
55             Console.WriteLine("========================================================");
56             Console.WriteLine(" > Nombre: {0}", nombre);
57             Console.WriteLine(" > Edad: {0}", edad);
58             Console.WriteLine(" > E-Mail: {0}", mail);
59             Console.WriteLine("========================================================");
60             Console.ReadKey();
61         }
62     }
63 }

Salida

olgadisima delhoyo

Tags

Artículos similares

Pruebas Unitarias con PHPUnit

PHPUnit es un framework que se utiliza para escribir tests en PHP, Netbeans nos permite configurarlo y usarlo fácilmente[...]

Tutorial Gráficos Vectoriales SVG – Parte I

Estructura Interna de un archivo SVG. <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD[...]

Formulario de login personalizado

Hola, en este oportunidad dejo a disposición un formulario de logueo que se me ocurrió de repente creo inspirado en las[...]

Resolución de pantalla con LibGDX

En este tutorial, veremos un poco más de lo que son las texturas en libGDX y dos maneras de añadirlas a nuestros juegos,[...]

Animación de bicho feo en java2D

Java2d nos permite manipular imagenes y asi poder crear animaciones sencillas como muestra el siguiente video. Puedes de[...]

Gráficos de tortas en iReport

Los gráficos de torta, también llamados gráficos de 360 grados o circulares, son gráficos estadísticos que se utilizan p[...]