Examples of working with binary and text files C#
Creation methods: ๐
FileStream(string filename, FileMode mode) ,
FileMode(Append,Create,CreateNew,Open,OpenOrCreate,Truncate)
File.Open(string file, FileMode mode),
File.OpenRead(string file), File.OpenWrite(string file);
Main properties: Length, Position
Main methods:
CopyTo(Stream destination),
int Read(byte[] array, int offset, int count),
long Seek(long offset, SeekOrigin origin),
void Write(byte[] array, int offset, int count)
ValueTask WriteAsync(byte[] array, int offset, int count),
Task CopyToAsync(Stream destination)
Helper classes: StreamReader and StreamWriter text BinaryWriter, BinaryReader binary data
SeekOrigin.Begin: the beginning of the file, SeekOrigin.End: the end of the file, SeekOrigin.Current: the current position in the file
fstream.Seek(-5, SeekOrigin.End);
Ways to use: ๐
try
{
fstream=
new FileStream(@"f.dat",
FileMode.OpenOrCreate);
}
catch(Exceptionex)
{
}
finally
{
if (fstream != null)
fstream.Close();
}
or using
using (var fs =
new FileStream(@"filePath",
FileModeโฆ, FileAccessโฆ))
{
}
Working with text files ๐
using System;
using System.Text;
using System.Threading.Tasks;
using System.IO;
FileStream fs =
new FileStream(@"ft.txt",
FileMode.OpenOrCreate, FileAccess.Write);
using (var sw = new StreamWriter(fs)){
sw.WriteLine(@"text\file");
}
fs.Close();
using(varfsr=
new FileStream(@"ft.txt", FileMode.Open,
FileAccess.Read))
{
using (var sr= new StreamReader(fsr))
{
varstr = sr.ReadLine();
Console Write(str);
}
}
Working with binary files ๐
using (var fs = new FileStream(@"fb.bin", FileMode.OpenOrCreate, FileAccess.Write)){
using (var bw = new BinaryWriter(fs))
{
bw Write((int)200);
}
}
using (var fs = File.OpenRead(@"fb.bin")){
using (var br = new BinaryReader(fs)){
varv = br.ReadInt32();
Console.WriteLine(v);
}
}