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
An array of streams in C#
An array of streams in C# refers to using arrays or collections along with stream objects like StreamWriter and StreamReader to perform file I/O operations. This approach is commonly used when you need to write multiple data items from an array to a file or read file content into an array structure.
Syntax
Following is the syntax for writing array data to a file using StreamWriter −
using (StreamWriter writer = new StreamWriter("filename.txt")) {
foreach (dataType item in arrayName) {
writer.WriteLine(item);
}
}
Following is the syntax for reading file content using StreamReader −
using (StreamReader reader = new StreamReader("filename.txt")) {
string line;
while ((line = reader.ReadLine()) != null) {
Console.WriteLine(line);
}
}
Writing Array Data to File
You can write array elements to a file by iterating through the array using a foreach loop and writing each element using StreamWriter −
using System;
using System.IO;
class Program {
static void Main(string[] args) {
string[] names = new string[] {"Jack", "Tom", "Alice", "Bob"};
using (StreamWriter sw = new StreamWriter("names.txt")) {
foreach (string name in names) {
sw.WriteLine(name);
}
}
Console.WriteLine("Array data written to file successfully!");
}
}
The output of the above code is −
Array data written to file successfully!
Reading File Content and Writing Array Data
This example demonstrates both writing array data to a file and then reading it back −
using System;
using System.IO;
class Program {
static void Main(string[] args) {
string[] names = new string[] {"Jack", "Tom"};
// Write array data to file
using (StreamWriter sw = new StreamWriter("names.txt")) {
foreach (string s in names) {
sw.WriteLine(s);
}
}
// Read and display each line from the file
Console.WriteLine("Contents of the file:");
string line = "";
using (StreamReader sr = new StreamReader("names.txt")) {
while ((line = sr.ReadLine()) != null) {
Console.WriteLine(line);
}
}
}
}
The output of the above code is −
Contents of the file: Jack Tom
Working with Multiple Data Types
You can also work with arrays containing different data types and format them appropriately when writing to streams −
using System;
using System.IO;
class Program {
static void Main(string[] args) {
int[] numbers = {10, 20, 30, 40, 50};
double[] prices = {19.99, 25.50, 12.75};
using (StreamWriter sw = new StreamWriter("data.txt")) {
sw.WriteLine("Numbers:");
foreach (int num in numbers) {
sw.WriteLine($"Value: {num}");
}
sw.WriteLine("\nPrices:");
foreach (double price in prices) {
sw.WriteLine($"${price:F2}");
}
}
// Read and display the file content
Console.WriteLine("File content:");
using (StreamReader sr = new StreamReader("data.txt")) {
Console.WriteLine(sr.ReadToEnd());
}
}
}
The output of the above code is −
File content: Numbers: Value: 10 Value: 20 Value: 30 Value: 40 Value: 50 Prices: $19.99 $25.50 $12.75
Conclusion
Arrays of streams in C# provide an efficient way to handle bulk data operations with files. By combining arrays with StreamWriter and StreamReader, you can easily write multiple values to files and read them back, making it useful for data persistence and file-based data processing scenarios.
