Advertisement
Methods | Description |
---|---|
void Write(int value) | This method writes a signed int value to the stream. |
void Write(uint s) | This method writes an unsigned int value to the stream. |
void Write(Single f) | This method writes a float to the stream. |
void Write(double d) | This method writes a double to the stream. |
void Write(long l) | This method writes a long to the stream. |
void Write(String str) | This method writes a String to the stream. |
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 WriteLine(int value) | This method writes a signed int value to the stream. |
void WriteLine(uint s) | This method writes an unsigned int value to the stream. |
void WriteLine(Single f) | This method writes a float to the stream. |
void WriteLine(double d) | This method writes a double to the stream. |
void WriteLine(long l) | This method writes a long to the stream. |
void WriteLine(String str) | This method writes a String to the stream. |
void WriteLine(char ch) | This method writes a character value to the stream. |
void WriteLine(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 WriteLine(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 TextWriter
using System;
using System.IO;
class A
{
public static void Main(String[] ar)
{
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 tr = File.CreateText("D:\\MyFile10.txt");
Console.WriteLine("Using the TextWriter to write to a file");
//writing an int
tr.Write(i);
//writing an uint
tr.Write(ui);
//writing a char
tr.Write(ch);
//writing a float
tr.Write(f);
//writing a double
tr.Write(d);
//writing a long
tr.Write(l);
//writing a String
tr.Write(str);
Console.WriteLine("Data written successfully");
//Calling the Flush() method to flush the buffered data to the file
tr.Flush();
//Closing the stream i.e. BinaryWriter stream
tr.Close();
}
catch(IOException e)
{
Console.WriteLine(e);
}
}
}
Using the TextWriter to write to a file
Data written successfully
-109C89.9875.88810000000000Keep smiling and think positive.
Advertisement
//C# Example of TextWriter
using System;
using System.IO;
class A
{
public static void Main(String[] ar)
{
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 tr = File.CreateText("D:\\MyFile11.txt");
Console.WriteLine("Using the TextWriter to write to a file");
//writing an int
tr.WriteLine(i);
//writing an uint
tr.WriteLine(ui);
//writing a char
tr.WriteLine(ch);
//writing a float
tr.WriteLine(f);
//writing a double
tr.WriteLine(d);
//writing a long
tr.WriteLine(l);
//writing a String
tr.WriteLine(str);
Console.WriteLine("Data written successfully");
//Calling the Flush() method to flush the buffered data to the file
tr.Flush();
//Closing the stream i.e. BinaryWriter stream
tr.Close();
}
catch(IOException e)
{
Console.WriteLine(e);
}
}
}
Using the TextWriter to write to a file
Data written successfully
-109C89.9875.88810000000000Keep smiling and think positive.
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement