Advertisement
Constructors | Description |
---|---|
StreamWriter(System.IO.Stream stream) | This constructor creates an object of the StreamWriter based on the specified stream and using the default UTF-8 encoding. |
FileStream(System.IO.Stream stream, System.Text.Encoding encoding) | The constructor created an object of the StreamWriter class based on the specified Stream and character encoding. |
Methods | Description |
---|---|
void Write(char ch) | This method writes a character value to the stream. |
void Write(char[] array) | This method writes a character array to the stream. If the array is null, then nothing is written to the output stream. |
void Write(char[] array, int index, int total) | This method writes a character array to the stream, where:
|
void Write(String str) | This method writes a String to the stream. |
void Flush() | This method clears the buffer for the current writer stream by writing any buffered data to the stream. |
void Close() | This method closes this stream and also frees any resources connected with this stream. |
//C# Example of StreamWriter by using the default UTF-8 encoding.
using System;
using System.IO;
class A
{
public static void Main(String[] ar)
{
try
{
//A character value
char ch = 'C';
//A character array
char[] arr = {'B', 'l', 'u', 'e', ' ', 'R' ,'a', 'i', 'n'};
//Creating a String object
String str = "Keep smiling and think positive.";
//Creating an object of FileStream to write to a file
//We are going to use the FileMode Create to create a new file to write to it
FileStream fs = new FileStream("D:\\MyFile1.txt", FileMode.Create);
//Creating an object of StreamWriter to wrap a byte outputStream i.e. FileStream
//to write to characters to the byte output stream.
StreamWriter sw = new StreamWriter(fs);
Console.WriteLine("Writing to the file using StreamWriter and default UTF-8 Encoding");
//writing a char
sw.Write(ch);
//writing a whole char array
sw.Write(arr);
//writing a part of char array
//Starting at its zeroth-index and a total of 4 characters
sw.Write(arr, 0 , 4);
//writing a String
sw.Write(str);
Console.WriteLine("Data written successfully");
//Calling the Flush() method to flush the buffered data to the file
sw.Flush();
//Closing the stream i.e. StreamWriter stream
//sw.Close();
}
catch(IOException e)
{
Console.WriteLine(e);
}
}
}
Writing to the file using StreamWriter and default UTF-8 Encoding
Data written successfully
CBlue RainBlueKeep smiling and think positive.
Advertisement
//C# Example of StreamWriter by using the non-default Unicode encoding.
//C# Example of StreamWriter
using System;
using System.IO;
using System.Text;
class A
{
public static void Main(String[] ar)
{
try
{
//A character value
char ch = 'C';
//A character array
char[] arr = {'B', 'l', 'u', 'e', ' ', 'R' ,'a', 'i', 'n'};
//Creating a String object
String str = "Keep smiling and think positive.";
//Creating an object of Encoding type
Encoding unicode = Encoding.Unicode;
//Creating an object of StreamWriter to wrap a byte outputStream i.e. FileStream
//to write to characters to the byte output stream.
FileStream fs = new FileStream("D:\\MyFile2.txt", FileMode.Create);
//Creating an object of StreamWriter to write to a file using FileStream
StreamWriter sw = new StreamWriter(fs, unicode);
Console.WriteLine("Writing to the file using StreamWriter and non-default Unicode Encoding");
//writing a char
sw.Write(ch);
//writing a whole char array
sw.Write(arr);
//writing a part of char array
//Starting at its zeroth-index and a total of 4 characters
sw.Write(arr, 0 , 4);
//writing a String
sw.Write(str);
Console.WriteLine("Data written successfully");
//Calling the Flush() method to flush the buffered data to the file
sw.Flush();
//Closing the stream i.e. StreamWriter stream
//sw.Close();
}
catch(IOException e)
{
Console.WriteLine(e);
}
}
}
Writing to the file using BinaryStream and non-default Unicode Encoding
Data written successfully
CBlue RainBlueKeep smiling and think positive.
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement