首先,我想澄清一下我不是非常精通 C#。在那,我正在使用 .Net 3.5 在 C# 中工作的项目让我构建一个类来读取和导出包含多个基于记录类型的固定宽度格式的文件。
目前有 5 种类型的记录,由文件每行中的第一个字符位置指示,它们指示特定的行格式。我遇到的问题是类型彼此不同。
Record type 1 has 5 columns, signifies beginning of the file
Record type 3 has 10 columns, signifies beginning of a batch
Record type 5 has 69 columns, signifies a transaction
Record type 7 has 12 columns, signifies end of the batch, summarizes
(these 3 repeat throughout the file to contain each batch)
Record type 9 has 8 columns, signifies end of the file, summarizes
这些固定宽度的文件有没有好的库?我见过一些想要将整个文件作为一个规范加载的好人,但那是行不通的。
每个月末读取大约 250 个这些文件,平均组合文件大小约为 300 兆。在这个项目中,效率对我来说非常重要。
根据我对数据的了解,我构建了一个我“认为”对象应该是什么样子的类层次结构......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Extract_Processing
{
class Extract
{
private string mFilePath;
private string mFileName;
private FileHeader mFileHeader;
private FileTrailer mFileTrailer;
private List<Batch> mBatches; // A file can have many batches
public Extract(string filePath)
{ /* Using file path some static method from another class would be called to parse in the file somehow */ }
public string ToString()
{ /* Iterates all objects down the heiarchy to return the file in string format */ }
public void ToFile()
{ /* Calls some method in the file parse static class to export the file back to storage somewhere */ }
}
class FileHeader
{ /* ... contains data types for all fields in this format, ToString etc */ }
class Batch
{
private string mBatchNumber; // Should this be pulled out of the batch header to make LINQ querying simpler for this data set?
private BatchHeader mBatchHeader;
private BatchTrailer mBatchTrailer;
private List<Transaction> mTransactions; // A batch can have multiple transactions
public string ToString()
{ /* Iterates through batches to return what the entire batch would look like in string format */ }
}
class BatchHeader
{ /* ... contains data types for all fields in this format, ToString etc */ }
class Transaction
{ /* ... contains data types for all fields in this format, ToString etc */ }
class BatchTrailer
{ /* ... contains data types for all fields in this format, ToString etc */ }
class FileTrailer
{ /* ... contains data types for all fields in this format, ToString etc */ }
}
我遗漏了许多构造函数和其他方法,但我认为这个想法应该非常可靠。我正在寻找对我正在考虑的方法的想法和批评,对 C# 不了解,执行时间是最高优先级。
除了一些批评之外,最大的问题是,我应该如何引入这个文件?我引入了许多其他语言的文件,例如使用 FSO 方法的 VBA、Microsoft Access ImportSpec 来读取文件(5 次,每个规范一个……哇,效率低下!),在visual foxpro (这是 FAAAAAAAST 但又一次,不得不做五次),但我正在寻找 C# 中隐藏的宝石,如果说的东西存在的话。
感谢您阅读我的小说,如果您在理解它时遇到问题,请告诉我。我正在利用周末来检查这个设计,看看我是否购买它,并想努力以这种方式实现它。