Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
File Objects in C#
The FileStream class in C# is used to create, read, write, and manipulate files. It provides low-level access to file operations and is part of the System.IO namespace. FileStream works with bytes, making it suitable for both text and binary files.
Syntax
Following is the syntax for creating a FileStream object −
FileStream objectName = new FileStream(fileName, FileMode, FileAccess, FileShare);
A simplified syntax for common scenarios −
FileStream objectName = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
Parameters
| Parameter | Description | Common Values |
|---|---|---|
| fileName | Path and name of the file | "data.txt", "C:\files\log.dat" |
| FileMode | How to open or create the file | Create, Open, OpenOrCreate, Append |
| FileAccess | Type of access to the file | Read, Write, ReadWrite |
| FileShare | How other processes can access the file | None, Read, Write, ReadWrite |
Using FileStream to Write and Read Binary Data
Example
using System;
using System.IO;
namespace FileIOApplication {
class Program {
static void Main(string[] args) {
FileStream F = new FileStream("test.dat", FileMode.OpenOrCreate, FileAccess.ReadWrite);
// Write bytes 1 to 20
for (int i = 1; i <= 20; i++) {
F.WriteByte((byte)i);
}
// Reset position to beginning
F.Position = 0;
// Read and display bytes
Console.WriteLine("Reading bytes from file:");
for (int i = 0; i < 20; i++) {
Console.Write(F.ReadByte() + " ");
}
F.Close();
Console.WriteLine("\nFile operations completed.");
}
}
}
The output of the above code is −
Reading bytes from file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 File operations completed.
Using FileStream with Text Data
Example
using System;
using System.IO;
using System.Text;
class Program {
static void Main() {
string fileName = "textfile.txt";
string message = "Hello, FileStream!";
// Write text to file
using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write)) {
byte[] data = Encoding.UTF8.GetBytes(message);
fs.Write(data, 0, data.Length);
Console.WriteLine("Data written to file.");
}
// Read text from file
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read)) {
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
string result = Encoding.UTF8.GetString(buffer);
Console.WriteLine("Data read from file: " + result);
}
}
}
The output of the above code is −
Data written to file. Data read from file: Hello, FileStream!
File Operations with Error Handling
Example
using System;
using System.IO;
class Program {
static void Main() {
FileStream fs = null;
try {
fs = new FileStream("demo.dat", FileMode.Create, FileAccess.Write);
// Write some data
for (int i = 65; i <= 90; i++) {
fs.WriteByte((byte)i); // ASCII A-Z
}
Console.WriteLine("File created and data written successfully.");
}
catch (IOException ex) {
Console.WriteLine("IO Error: " + ex.Message);
}
catch (Exception ex) {
Console.WriteLine("Error: " + ex.Message);
}
finally {
if (fs != null) {
fs.Close();
Console.WriteLine("File closed.");
}
}
}
}
The output of the above code is −
File created and data written successfully. File closed.
Conclusion
FileStream in C# provides direct, low-level access to files for reading and writing binary data. It offers precise control over file operations and is essential when working with binary files or when you need fine-grained control over file access. Always remember to close FileStream objects or use using statements for automatic disposal.
