Advertisement
Methods | Description |
---|---|
int Read() | This method reads the next character from the text reader 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 text reader, where:
This method returns the number of characters that have been read from the input string. |
String ReadLine() | This method reads a line of characters from the text reader and returns the data as a String, or null if the end of the input stream is reached. |
void Close() | This method closes the TextReader. |
//C# Example of TextReader
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 variables with primitives values
int i = -10;
uint ui = 9;
char ch = 'C';
float f = 89.9f;
double d = 875.888;
long l = 10000000000;
//Creating a String object
String str = "Keep smiling and think positive.";
//Calling the CreateText() method of the File class to create a new file and
//it returns an object of a type
//Which has implemented the TextWriter abstract class.
TextWriter tw = File.CreateText("D:\\MyFile10.txt");
Console.WriteLine("Using the TextWriter to write to a file");
//writing an int
tw.Write(i);
//writing an uint
tw.Write(ui);
//writing a char
tw.Write(ch);
//writing a float
tw.Write(f);
//writing a double
tw.Write(d);
//writing a long
tw.Write(l);
//writing a String
tw.Write(str);
Console.WriteLine("Data written successfully");
//Calling the Flush() method to flush the buffered data to the file
tw.Flush();
//Closing the stream i.e. BinaryWriter stream
tw.Close();
}
catch(IOException e)
{
Console.WriteLine(e);
}
}
public void ReadValues()
{
try
{
//Calling the OpenText() method of the File class to read an existing file and
//it returns an object of a type
//Which has implemented the TextWriter abstract class.
TextReader tr = File.OpenText("D:\\MyFile10.txt");
int c;
Console.WriteLine("Reading the characters by using the TextReader:");
while((c = tr.Read()) != -1)
{
Console.Write((char)c);
}
//Closing the stream i.e. TextReader stream
tr.Close();
}
catch(IOException e)
{
Console.WriteLine(e);
}
}
}
Using the TextWriter to write to a file
Data written successfully
Reading values from a file using BinaryStream in memory
Reading the characters by using the TextReader:
-109C89.9875.88810000000000Keep smiling and think positive.
-109C89.9875.88810000000000Keep smiling and think positive.
Advertisement
//C# Example of TextReader
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 variables with primitives values
int i = -10;
uint ui = 9;
char ch = 'C';
float f = 89.9f;
double d = 875.888;
long l = 10000000000;
//Creating a String object
String str = "Keep smiling and think positive.";
//Calling the CreateText() method of the File class to create a new file and
//it returns an object of a type
//Which has implemented the TextWriter abstract class.
TextWriter tw = File.CreateText("D:\\MyFile11.txt");
Console.WriteLine("Using the TextWriter to write to a file");
//writing an int
tw.Write(i);
//writing an uint
tw.Write(ui);
//writing a char
tw.Write(ch);
//writing a float
tw.Write(f);
//writing a double
tw.Write(d);
//writing a long
tw.Write(l);
//writing a String
tw.Write(str);
Console.WriteLine("Data written successfully");
//Calling the Flush() method to flush the buffered data to the file
tw.Flush();
//Closing the stream i.e. BinaryWriter stream
tw.Close();
}
catch(IOException e)
{
Console.WriteLine(e);
}
}
public void ReadValues()
{
try
{
//Calling the OpenText() method of the File class to read an existing file and
//it returns an object of a type
//Which has implemented the TextWriter abstract class.
TextReader tr = File.OpenText("D:\\MyFile11.txt");
Console.WriteLine("Reading a line of characters by using TextReader:");
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 = tr.ReadLine()) != null)
{
Console.WriteLine(s);
}
//Closing the stream i.e. TextReader stream
tr.Close();
}
catch(IOException e)
{
Console.WriteLine(e);
}
}
}
Using the TextWriter to write to a file
Data written successfully
Reading a line of characters by using TextReader:
-109C89.9875.88810000000000Keep smiling and think positive.
-109C89.9875.88810000000000Keep smiling and think positive.
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement