Created
August 29, 2013 08:47
-
-
Save tugberkugurlu/6375703 to your computer and use it in GitHub Desktop.
Read a file from a FileStream and get the bytes. Pass the bytes to a new method and try to read it there with the right encoding.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace StreamReadWrite | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string pathSource = @"c:\trash\foo.txt"; | |
using (FileStream fsSource = new FileStream(pathSource, | |
FileMode.Open, FileAccess.Read)) | |
{ | |
// Read the source file into a byte array. | |
byte[] bytes = new byte[fsSource.Length]; | |
int numBytesToRead = (int)fsSource.Length; | |
int numBytesRead = 0; | |
while (numBytesToRead > 0) | |
{ | |
// Read may return anything from 0 to numBytesToRead. | |
int n = fsSource.Read(bytes, numBytesRead, numBytesToRead); | |
// Break when the end of the file is reached. | |
if (n == 0) | |
break; | |
numBytesRead += n; | |
numBytesToRead -= n; | |
} | |
numBytesToRead = bytes.Length; | |
WriteConsole(bytes); | |
WriteConsoleBetter(bytes); | |
//string content = Encoding.UTF8.GetString(bytes); | |
//Console.WriteLine(content); | |
} | |
using (StreamReader reader = new StreamReader("c:\\trash\\foo.txt")) | |
{ | |
int readBytes = 0; | |
int increment = 100; | |
char[] arrayToFill = new char[reader.BaseStream.Length]; | |
while (reader.BaseStream.Length >= readBytes) | |
{ | |
int currentReadCount; | |
if ((readBytes + increment) > reader.BaseStream.Length) | |
{ | |
currentReadCount = (int)(reader.BaseStream.Length - readBytes); | |
} | |
else | |
{ | |
currentReadCount = increment; | |
} | |
reader.Read(arrayToFill, readBytes, currentReadCount); | |
readBytes += increment; | |
} | |
//var content = reader.ReadToEnd(); | |
string content = new string(arrayToFill); | |
Console.WriteLine(content); | |
} | |
Console.ReadLine(); | |
} | |
private static void WriteConsole(byte[] bytes) | |
{ | |
using (MemoryStream stream = new MemoryStream(bytes)) | |
using (StreamReader reader = new StreamReader(stream)) | |
{ | |
int readBytes = 0; | |
int increment = 100; | |
char[] arrayToFill = new char[reader.BaseStream.Length]; | |
while (reader.BaseStream.Length >= readBytes) | |
{ | |
int currentReadCount; | |
if ((readBytes + increment) > reader.BaseStream.Length) | |
{ | |
currentReadCount = (int)(reader.BaseStream.Length - readBytes); | |
} | |
else | |
{ | |
currentReadCount = increment; | |
} | |
reader.Read(arrayToFill, readBytes, currentReadCount); | |
readBytes += increment; | |
} | |
//var content = reader.ReadToEnd(); | |
string content = new string(arrayToFill); | |
Console.WriteLine(content); | |
} | |
} | |
private static void WriteConsoleBetter(byte[] bytes) | |
{ | |
using (MemoryStream stream = new MemoryStream(bytes)) | |
using (StreamReader reader = new StreamReader(stream)) | |
{ | |
// This is enough the change the encoding of the StreamReader in a right way. | |
reader.Read(new char[1], 0, 1); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment