The IOHelper doesn't require a game component to use. All it is is 3 static classes to aid in...
  • Writing text to files
  • Reading text from files
  • Serializing and Deserializing objects to and from Xml files

The class names are as follows...
  • FileWriter
  • FileReader
  • _Xml

Since the FileReader and FileWriter class are a bit simpler let's go over these first.
The FileWriter class is very straightfoward.
Here is a layout of how it works.
  1. Set Write Path - FileWriter.SetPath(string)
  2. Write to buffer - FileWriter.WriteLine(string) or FileWriter.Write(string)
  3. Write buffer to file - FileWriter.Flush()

Ok so here is an example of a basic Write operations.
public static void MyWriteFunction()
{
     FileWriter.SetPath(@"C:\Users\Andrew Black\Desktop\TestFile.txt");
     FileWriter.WriteLine("Hello World!");
     FileWriter.Flush();
}

That's really all there is to the FileWriter class.
Things to remember:
You have to call Flush() to write the buffer to the actual file. Just calling FileWriter.Write does NOT write to the file. it is writing it to a buffer.
Another thing, if you do not call flush then technically that file is still being controlled by it so nothing else will access it. You are not required to
check for errors but it is best to wrap FileWriter.SetPath(string); in a Try Catch setup, since on windows file access permission vary from system to system.
So it would look like this...
try
{
FileWriter.SetPath(@"C:\testFile.txt");
}
catch(Exception e)
{
     //exception handling.... 
}
finally
{
     //code to correct the error
}

Ok, now that we have covered the FileWriter, let's go over the FileReader.

It is simple to use but there are some considerations you need to know about.

So the basic setup is...
  1. SetPath
  2. Read text
  3. Close

The considerations for FileReader.SetPath(string) are the same as FileWriter.SetPath(string) so follow the same considerations.

Now to read the text.
Unlike the version that is in the full library there are no safety's when reading a file, you have to implement those yourself.
Ok now I will go over the 3 functions then give an example.
ReadLine, reads a line from the buffer and returns it as a string.
ReadAll, reads the entire file from the buffer and returns it as a string
Close, frees the file from the reader's control.

So here's what a simple read would look like.
public static void MyReadFileFunc()
{
     try
     {
           FileReader.SetPath(@"C:\TestFolder\TestFile.txt");
     }
     catch(Exception e)
     {
           throw new Exception("Cannot access that path");
     }

     while(true)
     {
           if(!FileReader.EndOfStream)
           {
                 Console.WriteLine(FileReader.ReadLine());
           }
           else
           {
                //If it's the end of the file then there's nothing more to read so break the loop
                return false;
           }               
     }
     FileReader.Close();
}

Alright that's about it for the File Reader.

Last but certainly not least!
the _Xml class.

This class only contains 2 functions but they are far from useless.

This is best explained through examples so here ya go.
public static void MyXmlFunction()
{
     Rectangle rect = new Rectangle(0,0,100,100);
     try
     {
          _Xml.Serialize<Rectangle>(@"C:\XmlFolder\rect.xml", rect);
     }
     catch(Exception e)
     {
          throw new Exception("There was an error writing the xml file");
     }
}

That's all you have to do to serialize a file.
You are not required to wrap it in a try catch but it is usually best to do so.

Now, to deserialize an xml file.
public static void MyXmlFunction()
{
     Rectangle myRectangle;
     try
     {
           myRectangle = _Xml.Deserialize<Rectangle>(@"C:\XmlFolder\rect.xml");
     }
    catch(Exception e)
    {
         throw new Exception("Invalid Xml File");
    }
}

That's really all there is to the _Xml class.
Note that classes are not automatically able to be serialized. You have to create them in a way that they can. I will most likely make a tutorial on my website (when I finally get it running) but until then there are plenty of great tutorials on the web.

I hope this explains how to use these well enough, If there are any questions then please post something in the Discussions and I will try to help you resolve the error.

Last edited Jul 22, 2010 at 7:16 PM by Destroyer229, version 2

Comments

No comments yet.