开发者

Iterate through files and read records

What is the fastest way to the following in C# 3.5 :

  1. Iterate through files in a directory
  2. Read the file's records (fixed length of 247 characters)
  3. Convert the fixed length str开发者_Python百科ing of each record to a Struct or Class.

Thanks


This would be relatively fast to write:

var myStructs = 
from file in Directory.GetFiles(".", "*.*", SearchOption.TopDirectoryOnly)
select ConvertFileToStructs(File.ReadAllText(file));

If this is the fastest way possible, performance-wise? Probably not, but it won't make a huge difference. What will impact the performance is the implementation of the deserialization within the ConvertFileToStructs() function. But to answer this, we need to know the specific format of your files.


Just read your comments. I would suggest the following parsing:

List<MyStruct> ConvertFileToStructs(string content, int[] mapping)
{
    var records = new List<MyStruct>();
    int length = content.Length();
    for(int i = 0; i < length; i += 247)
         records.Add(ConvertRecordToStruct(content.Substring(i,247), mapping));
    return records;
}

MyStruct ConvertRecordToStruct(string record, int[] mapping)
{
    MyStruct s;
    s.Field1 =  record.Substring(mapping[0], mapping[1]);
    //set other fields
    return s;
}

This code could probably be optimized for performance, but I don't think it would change things dramatically, especially because I/O to disk is involved and Substring() is pretty fast (see http://dotnetperls.com/substring). Of course you will have to test this on your machine.


custom class to handle files

   class customFile
        {
            string fileText;
            public string FileText
            {
                get { return fileText; }
                set { fileText = value; }
            }
        }

read all text

        string[] filePaths = Directory.GetFiles(dirPath);
        List<customFile> customFiles = new List<customFile>();
        foreach (string file in filePaths)
        {
            customFiles.Add(new customFile { FileText = File.ReadAllText(file) });
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜