logo

C# Dosya Akışı

C# FileStream sınıfı, dosya işlemi için bir akış sağlar. Senkron ve asenkron okuma ve yazma işlemlerini gerçekleştirmek için kullanılabilir. FileStream sınıfının yardımıyla kolaylıkla dosyaya veri okuyabilir ve yazabiliriz.

C# FileStream örneği: dosyaya tek bayt yazma

Tek baytlık veriyi dosyaya yazmak için FileStream sınıfının basit örneğini görelim. Burada okuma ve yazma işlemleri için kullanılabilecek OpenOrCreate dosya modunu kullanıyoruz.

 using System; using System.IO; public class FileStreamExample { public static void Main(string[] args) { FileStream f = new FileStream('e:\b.txt', FileMode.OpenOrCreate);//creating file stream f.WriteByte(65);//writing byte into stream f.Close();//closing stream } } 

Çıktı:

 A 

C# FileStream örneği: dosyaya birden çok bayt yazma

Döngü kullanarak birden fazla baytlık veriyi dosyaya yazmak için başka bir örnek görelim.

 using System; using System.IO; public class FileStreamExample { public static void Main(string[] args) { FileStream f = new FileStream(&apos;e:\b.txt&apos;, FileMode.OpenOrCreate); for (int i = 65; i <= 90; i++) { f.writebyte((byte)i); } f.close(); < pre> <p>Output:</p> <pre> ABCDEFGHIJKLMNOPQRSTUVWXYZ </pre> <h3>C# FileStream example: reading all bytes from file</h3> <p>Let&apos;s see the example of FileStream class to read data from the file. Here, ReadByte() method of FileStream class returns single byte. To all read all the bytes, you need to use loop.</p> <pre> using System; using System.IO; public class FileStreamExample { public static void Main(string[] args) { FileStream f = new FileStream(&apos;e:\b.txt&apos;, FileMode.OpenOrCreate); int i = 0; while ((i = f.ReadByte()) != -1) { Console.Write((char)i); } f.Close(); } } </pre> <p>Output:</p> <pre> ABCDEFGHIJKLMNOPQRSTUVWXYZ </pre></=>

C# FileStream örneği: dosyadaki tüm baytları okuma

Dosyadan veri okumak için FileStream sınıfı örneğini görelim. Burada FileStream sınıfının ReadByte() yöntemi tek bayt döndürür. Tüm baytları okumak için döngüyü kullanmanız gerekir.

Java'yı listele
 using System; using System.IO; public class FileStreamExample { public static void Main(string[] args) { FileStream f = new FileStream(&apos;e:\b.txt&apos;, FileMode.OpenOrCreate); int i = 0; while ((i = f.ReadByte()) != -1) { Console.Write((char)i); } f.Close(); } } 

Çıktı:

 ABCDEFGHIJKLMNOPQRSTUVWXYZ