
November 3rd, 2007, 08:01 PM
|
|
Registered User
|
|
Join Date: Nov 2007
Posts: 3
Time spent in forums: 43 m 39 sec
Reputation Power: 0
|
|
If you can do with a program that does not require reading the file one character at a time, then here is a solution:
Code:
using System;
using System.IO;
class ReadWriteExample
{
static void Main(string[] args)
{
// Use FileStream to specify the files to use for reading and writing
FileStream readFromThisFile = new FileStream(@"C:\abc.txt", FileMode.OpenOrCreate, FileAccess.Read);
FileStream writeToThisFile = new FileStream(@"C:\xyz.txt", FileMode.OpenOrCreate, FileAccess.Write);
// Create a stream called reader to read the data
StreamReader reader = new StreamReader(readFromThisFile);
// Put all of the data into the variable called contentsOfTextFile
string contentsOfTextFile = reader.ReadToEnd();
// Create a stream called Writer to write the data
StreamWriter writer = new StreamWriter(writeToThisFile);
// Put all the data which is in contentsOfTextFile to that file
writer.Write(contentsOfTextFile);
// Close the resources
writer.Close();
reader.Close();
writeToThisFile.Close();
readFromThisFile.Close();
}
}
Although, this reads the entire file into one string rather than one character at a time.
|