Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 17th, 2012  |  syntax: None  |  size: 2.07 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5.  
  6. namespace Illumine
  7. {
  8.  public class TextDocument
  9.  {
  10.   private String _text;
  11.   private String _filePath;
  12.  
  13.   /// <summary>
  14.   /// Constructor for the TextDocument class
  15.   /// </summary>
  16.   public TextDocument ()
  17.   {
  18.    _text = "";
  19.    _filePath = "";
  20.   }
  21.  
  22.   /// <summary>
  23.   /// Constructor for the TextDocument class
  24.   /// Will attempt to load the text file into memory
  25.   /// </summary>
  26.   /// <param name="documentPath">A path to a file</param>
  27.   public TextDocument ( String documentPath )
  28.   {
  29.    _filePath = documentPath;
  30.  
  31.    // Initializing a StreamReader object. We will attempt to figure out what the encoding is via the BOM if any.
  32.    StreamReader sr = new StreamReader ( _filePath, true );
  33.  
  34.    try
  35.    {
  36.         String str = "";
  37.         do
  38.         {
  39.          str += sr.ReadLine ();
  40.         } while ( !sr.EndOfStream );
  41.  
  42.         _text = str;
  43.    }
  44.    catch ( System.IO.IOException e )
  45.    {
  46.         System.Diagnostics.Debug.Print ( e.TargetSite + ": " + e.Message );
  47.    }
  48.    finally
  49.    {
  50.         sr.Close ();
  51.    }
  52.   }
  53.  
  54.   /// <summary>
  55.   /// Constructor for the TextDocument class
  56.   /// Will attempt to load the text file into memory
  57.   /// </summary>
  58.   /// <param name="documentPath">A path to a file</param>
  59.   /// <param name="documentStream">A stream that points to a file; should be derived from the filePath.</param>
  60.   public TextDocument ( String documentPath, Stream documentStream )
  61.   {
  62.    _filePath = documentPath;
  63.  
  64.    // Initializing a StreamReader object. We will attempt to figure out what the encoding is via the BOM if any.
  65.    StreamReader sr = new StreamReader ( documentStream, true );
  66.  
  67.    try
  68.    {
  69.         String str = "";
  70.         do
  71.         {
  72.          str += sr.ReadLine ();
  73.         } while ( !sr.EndOfStream );
  74.  
  75.         _text = str;
  76.    }
  77.    catch ( System.IO.IOException e )
  78.    {
  79.         System.Diagnostics.Debug.Print ( e.TargetSite + ": " + e.Message );
  80.    }
  81.    finally
  82.    {
  83.         sr.Close ();
  84.    }
  85.   }
  86.  
  87.   public String GetDocument ()
  88.   {
  89.    return _text;
  90.   }
  91.  
  92.   public String GetFilepath ()
  93.   {
  94.    return _filePath;
  95.   }
  96.  
  97.  }
  98. }