ファイルを解析する前に列が不明な場合、区切りファイルを解析する最良の方法は何ですか?
ファイル形式は Rightmove v3 (.blm) で、構造は次のようになります。
#HEADER#
Version : 3
EOF : '^'
EOR : '~'
#DEFINITION#
AGENT_REF^ADDRESS_1^POSTCODE1^MEDIA_IMAGE_00~ // can be any number of columns
#DATA#
agent1^the address^the postcode^an image~
agent2^the address^the postcode^^~ // the records have to have the same number of columns as specified in the definition, however they can be empty
etc
#END#
ファイルは非常に大きくなる可能性があります。私が持っているサンプル ファイルは 40Mb ですが、数百メガバイトになる可能性があります。以下は、列が動的であることに気付く前に開始したコードです。大きなファイルを処理するための最良の方法であると読んだときに、ファイルストリームを開いています。すべてのレコードをリストに入れて処理するという私の考えはよくわかりませんが、それがそのような大きなファイルで機能するかどうかはわかりません。
List<string> recordList = new List<string>();
try
{
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
StreamReader file = new StreamReader(fs);
string line;
while ((line = file.ReadLine()) != null)
{
string[] records = line.Split('~');
foreach (string item in records)
{
if (item != String.Empty)
{
recordList.Add(item);
}
}
}
}
}
catch (FileNotFoundException ex)
{
Console.WriteLine(ex.Message);
}
foreach (string r in recordList)
{
Property property = new Property();
string[] fields = r.Split('^');
// can't do this as I don't know which field is the post code
property.PostCode = fields[2];
// etc
propertyList.Add(property);
}
これをより良くする方法のアイデアはありますか?それが役立つ場合は、C# 3.0 と .Net 3.5 です。
ありがとう、
アネリー