-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileStream.cs
37 lines (31 loc) · 1.09 KB
/
FileStream.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System;
using System.IO;
class Program{
static void Main(string[] args){
Console.WriteLine("Introduce una cadena de al menos 3 lineas: ");
string cadena = Console.ReadLine();
using (Stream strea = new MemoryStream()){
foreach (char caracter in cadena){
byte[] bytes = BitConverter.GetBytes(caracter);
strea.WriteByte(bytes[0]);
strea.WriteByte(bytes[1]);
}
using (FileStream archivo = new FileStream("archivo.txt", FileMode.Create)){
strea.Position = 0;
strea.CopyTo(archivo);
}
}
using (FileStream archivo = new FileStream("archivo.txt", FileMode.Open)){
Console.WriteLine("\nContenido del archivo:");
while (archivo.Position < archivo.Length){
byte[] tama = new byte[2];
int bytesRead = archivo.Read(tama, 0, tama.Length);
if (bytesRead == 2){
char caracter = BitConverter.ToChar(tama, 0);
Console.Write(caracter);
}
}
Console.WriteLine("\n");
}
}
}