Tutorial C# Sharp - Learn to program with performing exercises C# Sharp

Tutorial C# Sharp

Learn to program with performing exercises C# Sharp

ArrayList duplicate a text file - Tutorial C# Sharp
Tutorial C# Sharp4,85581245

ArrayList duplicate a text file - Tutorial C# Sharp


Lesson 11:

Dynamic memory management


Exercise 11.6:

ArrayList duplicate a text file


Objetive:

Create a program that reads from a text file and stores to another text file inverting the lines.

So, an input text file like:

ayer el Madrid
le ganĂ³
al Barcelona

will be stored in an output text file like:

al Barcelona
le ganĂ³
ayer el Madrid


Source Code:


using System;
using System.IO;
using System.Collections;
namespace TextFileInvert
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Introduce el nombre del fichero: ");
            string nombreArchivo = Console.ReadLine();

            if (!File.Exists(nombreArchivo))
            {
                Console.Write("El archivo no existe!");
                return;
            }

            try
            {
                StreamReader miArchivo;
                miArchivo = File.OpenText(nombreArchivo);
                string line;

                ArrayList miLista = new ArrayList();

                do
                {
                    line = miArchivo.ReadLine();
                    if (line != null)
                        miLista.Add(line);
                }
                while (line != null);

                miArchivo.Close();

                StreamWriter miArchivoAlReves = File.CreateText(
                    nombreArchivo + "-reverse.txt");

                int tamanyoArchivo = miLista.Count;
                for (int i = tamanyoArchivo - 1; i >= 0; i--)
                {
                    miArchivoAlReves.WriteLine(miLista[i]);
                }

                miArchivoAlReves.Close();

            }
            catch (Exception e)
            {
                Console.WriteLine("Error, " + e.Message);
            }
            Console.ReadLine();
        }
    }
}
Exercisey 11.6






Privacy Policy:



Google uses associated advertising companies to serve ads when it visits our website. These companies may use the information they obtain from your visits to this and other websites (not including your name, address, email address, or phone number) to provide you with announcements about products and services that interest you. If you would like to learn more about this practice and know your options to prevent these companies from using this information. Click in... Privacy and Terms of Google.

Cookies

This site uses Google cookies to provide its services, to personalize advertisements and to analyze traffic. Google receives information about your use of this website. More information in... Privacy and Terms of Google.