c# - Read appended information in text file -
i have text file have information appended, , want read information list. here design of text file.
------->26/05/2015 17:15:52<------------------ index :0-0 index :1-0 index :2-20150527 index :3-182431 ------->27/05/2015 17:15:52<------------------ index :0-0 index :1-0 index :2-20150527 index :3-182431 ------->28/05/2015 17:15:52<------------------ index :0-0 index :1-0 index :2-20150527 index :3-182431
my question how can read information list, know can use line line how can know reading new item?
first should define word "new" if means:
- not read far in previous iterations
- new section in file
assuming mean new section in file can define such class representing item:
class item { public list<string> indexes; public string header; public item() { indexes= new list<string>(); } }
and parse file using simple loop this:
list<item> items = new list<item>(); var lines = file.readalllines("path-to-file"); item currentitem = null; foreach (var line in lines) { if (line.startswith("------->")) { 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);
if mean new not read far should store in "item" class date of entry , compare read entry date existing in collection , read new one.
also should consider if file gets cleared (rotates) time time, must decide if reading whole file makes sense or should read lines not read far using variable store number of lines read in previous iteration. , other things this.
Comments
Post a Comment