8.04 - Extended TextToHTML (files)

Objetive

Expand the TextToHtml class, so that ir can dump it result to a text file. Create a method ToFile, which will receive the name of the file as a parameter.

Hint: You must use a "StreamWriter"


Solution

using System;
using System.IO;

namespace ConvertTextToHtml
{
    class TextToHTML
    {
        private string[] text;
        private int numberLines;
        private int countLine;
       
        public TextToHTML() 
        {
            numberLines = 1000;
            text = new string[numberLines];
            countLine = 0;
        }

        public void ToFile(string nameFile) 
        {
            StreamWriter myfile;
            myfile = File.CreateText(nameFile);
            myfile.WriteLine(ToString());
            myfile.Close();
        }

        public void Add(string text) 
        {
            if (countLine < numberLines)
            {
                this.text[countLine] = "" + text + "
"; countLine++; } } public void Display() { Console.WriteLine(""); Console.WriteLine(""); for (int i = 0; i < countLine; i++) Console.WriteLine(text[i]); Console.WriteLine(""); Console.WriteLine("
"); } public override string ToString() { string textHtml=""; textHtml += ""; textHtml += ""; for (int i = 0; i < countLine; i++) textHtml += text[i]; textHtml += ""; textHtml += ""; return textHtml; } } }
using System; namespace ConvertTextToHtml { class Program { static void Main() { TextToHTML txt = new TextToHTML(); txt.Add("Hola que tal"); txt.Add("jajaja"); txt.Display(); txt.ToFile("prueba.html"); //Console.ReadLine(); } } }


Popular Posts