Posted by: ayhan | 25/04/2016

Read File Header Item Data

Header ve linelardan oluşan bir text dosyasını istediğiniz bir belirteç ile (header olduğunu belirten)
belirtip satır satır okuyabileceğiniz kod bloğu aşağıdaki şekildedir.

namespace ReadFile
{
    class Item
    {
        public List<string> Indexes;
        public string Header;

        public Item()
        {
            Indexes = new List<string>();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            List<Item> items = new List<Item>();

            var lines = File.ReadAllLines("../../TestFile1.txt");
            Item currentItem = null;
            foreach (var line in lines)
            {
                if (line.StartsWith("H"))
                {
                    if (currentItem != null)
                    {
                        items.Add(currentItem);
                    }
                    currentItem = new Item();
                    currentItem.Header = line;
                }
                else if (currentItem != null)
                {
                    currentItem.Indexes.Add(line);
                }
            }
            if (currentItem != null)
                items.Add(currentItem);
        }
    }
}

TestFile1
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: