4
我正在使用下面的代碼將大文本文件分解爲基於您可以在此處看到的邏輯的較小文件。我在File.WriteAllText行收到一條錯誤消息,說tempfile不存在。該流程是一個標題記錄,其後是多個報告數據行,隨後是報告行的一端,然後重新開始。有誰知道爲什麼我的臨時文件不會在這裏創建,我錯過了什麼?謝謝。正在創建Streamwriter文件
private static void SplitFile()
{
StreamReader sr = new StreamReader($"{_processDir}{_processFile}");
StreamWriter sw = null;
string fileName = string.Empty;
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
if (line.Split('\t')[0] == "FILEIDENTIFIER")
{
//line is a header record
sw = new StreamWriter("{_processDir}tempfile.txt", false);
sw.WriteLine(line);
}
else if (line.Contains("END OF\tREPORT"))
{
//line is end of report
sw.Close();
File.WriteAllText($"{_processDir}{fileName}.txt", File.ReadAllText($"{_processDir}tempfile.txt"));
}
else
{
//line is a report datarow
fileName = line.Split('\t')[0];
sw.WriteLine(line);
}
}
}
是@LolCoder아카쉬這是問題。如果我的代碼給了我一個關於該行的錯誤,這將會容易得多。我不確定爲什麼它沒有到達那裏,或者到達那裏時發生了什麼,但是非常感謝你爲我提供幫助的額外雙眼。 – Gavin
有時候,它發生了......很高興幫助你:) –
@Gavin它沒有給你一個錯誤,因爲'{_processDir} tempfile.txt'是一個有效的文件名。 –