Error reading text file line by line

Asked

Viewed 112 times

2

I am trying to read a very large file row by row and add in a list, but when it arrives at a certain point I get an error: System.OutOfMemoryException

Is there any way to read this whole file in a single time?

while ((line = trPlaces.ReadLine()) != null)
{
    var htmlLine = line.Split('\t');

    try
    {
        var newPlace = new Place
        {
            Id = htmlLine[0],
            Name = htmlLine[2],
            IsoCountry = htmlLine[8],
            Latitude = htmlLine[4],
            Longitude = htmlLine[5],
            Admin1 = htmlLine[10].Length == 1 ? "0" + htmlLine[10] : htmlLine[10],
            Admin2 = htmlLine[11],
            PlaceType = htmlLine[7],
            AlternativeNames = htmlLine[3].Split(',')
        };

        if(listPlaces1.Count< 4000000)
        {
            listPlaces1.Add(newPlace);
        } else
        {
            listPlaces2.Add(newPlace);
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }
}

1 answer

1

An option to read the whole file at once and load it into an array is:

var arrLinhas = File.ReadAllLines(@"C:\Desenv\Texto.txt"); 

A suggestion to save memory would be nay load all objects into the list at once. You could create a class to instantiate objects only when they are used.

For example:

public class PlaceFactory
{
    private string[] _places;

    public PlaceFactory(string filepath)
    {
        _places = File.ReadAllLines(filepath);  
    }

    public Place ObterPlace(int index)
    {       
        var htmlLine = _places[index].Split('\t');

        var place = new Place()
        {
            Id = htmlLine[0],
            Name = htmlLine[2],
            IsoCountry = htmlLine[8],
            Latitude = htmlLine[4],
            Longitude = htmlLine[5],
            Admin1 = htmlLine[10].Length == 1 ? "0" + htmlLine[10] : htmlLine[10],
            Admin2 = htmlLine[11],
            PlaceType = htmlLine[7],
            AlternativeNames = htmlLine[3].Split(',')
        };          

        return place;
    }
}

class App
{
    static void Main()
    {
        var placeFactory = new PlaceFactory(@"C:\Desenv\Texto.txt");

        Place place;

        place = placeFactory.ObterPlace(2);
        place = placeFactory.ObterPlace(0);
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.