Here is the WriteFile class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO; //necessary for file creation
//and reading
namespace FileHandling
{
class WriteFiles
{
/// <summary>
/// This class writes a text file
/// the constructor takes the path
/// --the complete file name--
/// and instantiates the StreamWriter
/// object that writes the files
/// </summary>
private StreamWriter writer;
public WriteFiles(string path)
{
//instantiate the SteamWriteObject
//its constructor takes the path
//the true means set the file to append
//if it exists. If it doesn't exist it
//will create the file
//false would mean to write over
//the file if it exists
writer = new StreamWriter(path, true);
}
public void AddToFile(string line)
{
//this uses a method of the StreamWriter
//that writes a line to the file
writer.WriteLine(line);
}
public void CloseFile()
{
//this closes the file
//if you don't close it
//you will be unable to access
//the file
writer.Close();
}
}
}
Here is the ReadFile class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;//nescessary for file IO
namespace FileHandling
{
class ReadFile
{
/// <summary>
/// this class reads text files.
/// It takes the path to the file as a parameter
/// in the constructor
/// </summary>
///
private StreamReader reader;
private string filePath;
//constructor takes in the path
//as parameter
public ReadFile (string path)
{
filePath = path;
}
public string GetFile()
{
//this method gets the file and
//reads it, returning a string
string line = null;
//when dealing with things like files
//it is a good idea to use a try set
//It is always possible that the file
//is not at the specified path
//or that it is unreadable
try
{
//get the file from the location
//indicated by the path
reader = new StreamReader(filePath);
//read it all into a string
//this is not very sophisticated
//there are other ways to read the file
line = reader.ReadToEnd();
}
catch (FileNotFoundException fnf)
{
//the FileNotFoundException is a pre built
//exception for missing files
//we throw the exception to the
//calling class for display
throw fnf;
}
catch (Exception ex)
{
//this catch is for any other kind
//of error
throw ex;
}
return line;
}
}
}
Here is the Program class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FileHandling
{
class Program
{
/// <summary>
/// I use this class to call the other classes
/// The WriteStuff() method writes a file
/// and the ReadStuff() method reads the
/// file that was just written
/// </summary>
///
static void Main(string[] args)
{
Program p = new Program();
p.WriteStuff();
p.ReadStuff();
Console.ReadKey();
}
private void WriteStuff()
{
//instantiate WriteFiles and pass the path
//to its constructor
WriteFiles write = new WriteFiles(@"C:\temp\MyFile.txt");
//write something to store in the file
Console.WriteLine("Enter whatever");
string stuff = Console.ReadLine();
//pass what you write to the WriteFiles method AddToFile(string)
write.AddToFile(stuff);
//make sure to close the file
write.CloseFile();
}
private void ReadStuff()
{
//this try catch will catch the errors
//thrown by ReadFiles
try
{
//first we read the file. If it doesn't exist
//it will throw a file not found exception
ReadFile read = new ReadFile(@"C:\temp\MyFile.txt");
//now we read the file into a string
string myText = read.GetFile();
//then we display the string
Console.WriteLine(myText);
}
catch(Exception ex)
{
//this catches any error and displays
//the error message
Console.WriteLine(ex.Message);
Console.ReadKey();
}
}
}
}
No comments:
Post a Comment