Advertisement
Constructors | Description |
---|---|
StreamReader(System.IO.Stream stream) | This constructor creates an object of the StreamReader 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 StreamReader class based on the specified Stream and character encoding. |
Methods | Description |
---|---|
int Read() | This method reads the next character from the input stream as an Int32 object, or -1 if no more characters are available. |
int Read(char[] array, int index, int total) | This method reads a character array from the input stream, where:
This method returns the number of characters that have been read. |
String ReadLine() | This method reads a line of characters from the current stream and returns the data as a String, or null if the end of the input stream is reached. |
void Close() | This method closes this character stream and also the wrapped byte stream and frees any resources connected with these streams. |
Don't give up on your dreams! Work hard for them!
//C# Example of StreamReader to read characters(one character at a time), from a file.
using System;
using System.IO;
using System.Text;
class A
{
public static void Main(String[] ar)
{
try
{
//Creating an object of FileStream to read to a file
//We are going to use the FileMode Open to open a new file to read from it
FileStream fs = new FileStream("D:\\MyFile4.txt", FileMode.Open);
//Creating an object of StreamReader to read to a file by wrapping the FileStream
//To read characters from it(one character at a time)
StreamReader sr = new StreamReader(fs);
Console.WriteLine("Reading characters(one character at a time) from a file using StreamReader");
int c;
while((c = sr.Read()) != -1)
{
Console.Write((char)c);
}
//Closing the stream i.e. StreamReader stream
sr.Close();
}
catch(IOException e)
{
Console.WriteLine(e);
}
}
}
Reading characters(one character at a time) from a file using StreamReader
Don't give up on your dreams! Work hard for them!
Advertisement
Don't give up on your dreams! Work hard for them!
Keep Smiling!
Please, be kind and help whoever you can.
//C# Example of StreamReader to read characters(one character at a time), from a file.
using System;
using System.IO;
using System.Text;
class A
{
public static void Main(String[] ar)
{
try
{
//Creating an object of FileStream to read to a file
//We are going to use the FileMode Open to open a new file to read from it
FileStream fs = new FileStream("D:\\MyFile5.txt", FileMode.Open);
//Creating an object of StreamReader to read to a file by wrapping the FileStream
//To read characters from it(one character at a time)
StreamReader sr = new StreamReader(fs);
Console.WriteLine("Reading a line of characters from a file using StreamReader:");
String s;
//Calling the ReadLine() method which returns the String that is read
//It returns null if there is nothing to read anymore
while((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
//Closing the stream i.e. StreamReader stream
sr.Close();
}
catch(IOException e)
{
Console.WriteLine(e);
}
}
}
Reading a line of characters from a file using StreamReader:
Don't give up on your dreams! Work hard for them!
Keep Smiling!
Please, be kind and help whoever you can.
//C# Example of StreamReader to read a line of characters using non-default Unicode encoding.
//This example first uses StreamWriter to write characters using the non-defau.t Unicode encoding.
//And then, it uses the StreamReader to read a line of characters using the same non-default Unicode encoding.
using System;
using System.IO;
using System.Text;
class A
{
public static void Main(String[] ar)
{
A ob = new A();
ob.WriteValues();
ob.ReadValues();
}
public void WriteValues()
{
try
{
//Creating a char value
char ch = 'C';
//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 FileStream to write to a file
//We are going to use the FileMode Create to create a new file
FileStream fs = new FileStream("D:\\MyFile5.txt", FileMode.Create);
//Creating an object of StreamWriter to write characters to a file
//By wrapping the FileStream
StreamWriter bw = new StreamWriter(fs, unicode);
Console.WriteLine("Writing characters to the file using Streamwriter, based on Unicode encoding");
//writing a char
bw.Write(ch);
//writing a String
bw.Write(str);
//Calling the Flush() method to flush the buffered characters to the file
bw.Flush();
//Closing the stream i.e. BinaryWriter stream
bw.Close();
}
catch(IOException e)
{
Console.WriteLine(e);
}
}
public void ReadValues()
{
try
{
//Creating an object of Encoding type
Encoding unicode = Encoding.Unicode;
//Creating an object of FileStream to read to a file
//We are going to use the FileMode Open to open a new file to read from it
FileStream fs = new FileStream("D:\\MyFile5.txt", FileMode.Open);
//Creating an object of StreamReader to read to a file by wrapping the FileStream
//To read characters from it(one character at a time)
StreamReader sr = new StreamReader(fs, unicode);
Console.WriteLine("Reading characters from a file using StreamReader, based on Unicode encoding");
String s;
//Calling the ReadLine() method which returns the String that is read
//It returns null if there is nothing to read anymore
while((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
//Closing the stream i.e. StreamReader stream
sr.Close();
}
catch(IOException e)
{
Console.WriteLine(e);
}
}
}
Writing characters to the file using StreamWriter, based on Unicode encoding
Reading characters from a file using StreamReader, based on Unicode encoding
CKeep smiling and think positive.
C @K e e p s m i l i n g a n d t h i n k p o s i t i v e .
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement