添加第一行写入外部文件的文本
Add text with the 1st line that is written to an external file
这是我的问题...
我正在验证文本文件中的行长度,一次一行。如果长度 <= 2033,则进行处理。如果它 > 2033,它会进入不同的文件。
我希望能够在错误文件的顶部添加一些信息。但是,我只想在要添加的第一行中添加文本。这就是我到目前为止所拥有的:
//Pass the file path and file name to the StreamReader and StreamWriter constructors
StreamReader sr = new StreamReader(inputFile);
StreamWriter sw = new StreamWriter(Dts.Connections["CE802CleanInput"].ConnectionString);
StreamWriter swe = new StreamWriter(Dts.Connections["CE802PreValidationErrors"].ConnectionString);
//Read the first line
line = sr.ReadLine();
while (line != null)
{
int length = line.Length;
if (length > 2033)
{
if // THIS IS WHERE I WOULD HAVE TO ADD THE CONDITION
{
swe.WriteLine("Some records have been rejected at the pre validation phase.");
swe.WriteLine("Those records will not be included in the process.");
swe.WriteLine("Please review the records below, fix and re submit if applicable.");
swe.WriteLine("Input file: " + Dts.Connections["CE802Input"].ConnectionString.ToString());
swe.WriteLine();
swe.WriteLine(line);
count++;
}
else
{
swe.WriteLine(line);
count++;
}
}
if (length <= 2033)
{
sw.WriteLine(line);
}
line = sr.ReadLine();
}
添加一个布尔运算符,只有第一次才做额外的行,然后设置它下次不写,但总是为坏数据写你的行
//Pass the file path and file name to the StreamReader and StreamWriter constructors
StreamReader sr = new StreamReader(inputFile);
StreamWriter sw = new StreamWriter(Dts.Connections["CE802CleanInput"].ConnectionString);
StreamWriter swe = new StreamWriter(Dts.Connections["CE802PreValidationErrors"].ConnectionString);
bool writeHeaderLine = false;
//Read the first line
line = sr.ReadLine();
while (line != null)
{
int length = line.Length;
if (length > 2033)
{
// only do this if it is false/first time
if(writeHeaderLine == false) // THIS IS WHERE I WOULD HAVE TO ADD THE CONDITION
{
swe.WriteLine("Some records have been rejected at the pre validation phase.");
swe.WriteLine("Those records will not be included in the process.");
swe.WriteLine("Please review the records below, fix and re submit if applicable.");
swe.WriteLine("Input file: " + Dts.Connections["CE802Input"].ConnectionString.ToString());
swe.WriteLine();
writeHeaderLine = true;
}
// always do this
swe.WriteLine(line);
count++;
}
if (length <= 2033)
{
sw.WriteLine(line);
}
line = sr.ReadLine();
}
using (StreamReader sr = new StreamReader(inputFile))
{
using (StreamWriter sw = new StreamWriter(Dts.Connections["CE802CleanInput"].ConnectionString))
{
using (StreamWriter swe = new StreamWriter(Dts.Connections["CE802PreValidationErrors"].ConnectionString));
{
swe.WriteLine("Some records have been rejected at the pre validation phase.");
swe.WriteLine("Those records will not be included in the process.");
swe.WriteLine("Please review the records below, fix and re submit if applicable.");
swe.WriteLine("Input file: " + Dts.Connections["CE802Input"].ConnectionString.ToString());
while (line = sr.ReadLine() != null)
{
int length = line.Length;
if (length > 2033)
{
swe.WriteLine();
swe.WriteLine(line);
count++;
}
else
{
sw.WriteLine(line);
}
}
}
}
}
是的!!!谢谢:-)
bool firstError = true;
while (line != null)
{
int length = line.Length;
if (length > 2033)
{
if (firstError)
{
firstError = false;
swe.WriteLine("Some records have been rejected at the pre validation phase.");
// etc. etc.
这是我的问题...
我正在验证文本文件中的行长度,一次一行。如果长度 <= 2033,则进行处理。如果它 > 2033,它会进入不同的文件。
我希望能够在错误文件的顶部添加一些信息。但是,我只想在要添加的第一行中添加文本。这就是我到目前为止所拥有的:
//Pass the file path and file name to the StreamReader and StreamWriter constructors
StreamReader sr = new StreamReader(inputFile);
StreamWriter sw = new StreamWriter(Dts.Connections["CE802CleanInput"].ConnectionString);
StreamWriter swe = new StreamWriter(Dts.Connections["CE802PreValidationErrors"].ConnectionString);
//Read the first line
line = sr.ReadLine();
while (line != null)
{
int length = line.Length;
if (length > 2033)
{
if // THIS IS WHERE I WOULD HAVE TO ADD THE CONDITION
{
swe.WriteLine("Some records have been rejected at the pre validation phase.");
swe.WriteLine("Those records will not be included in the process.");
swe.WriteLine("Please review the records below, fix and re submit if applicable.");
swe.WriteLine("Input file: " + Dts.Connections["CE802Input"].ConnectionString.ToString());
swe.WriteLine();
swe.WriteLine(line);
count++;
}
else
{
swe.WriteLine(line);
count++;
}
}
if (length <= 2033)
{
sw.WriteLine(line);
}
line = sr.ReadLine();
}
添加一个布尔运算符,只有第一次才做额外的行,然后设置它下次不写,但总是为坏数据写你的行
//Pass the file path and file name to the StreamReader and StreamWriter constructors
StreamReader sr = new StreamReader(inputFile);
StreamWriter sw = new StreamWriter(Dts.Connections["CE802CleanInput"].ConnectionString);
StreamWriter swe = new StreamWriter(Dts.Connections["CE802PreValidationErrors"].ConnectionString);
bool writeHeaderLine = false;
//Read the first line
line = sr.ReadLine();
while (line != null)
{
int length = line.Length;
if (length > 2033)
{
// only do this if it is false/first time
if(writeHeaderLine == false) // THIS IS WHERE I WOULD HAVE TO ADD THE CONDITION
{
swe.WriteLine("Some records have been rejected at the pre validation phase.");
swe.WriteLine("Those records will not be included in the process.");
swe.WriteLine("Please review the records below, fix and re submit if applicable.");
swe.WriteLine("Input file: " + Dts.Connections["CE802Input"].ConnectionString.ToString());
swe.WriteLine();
writeHeaderLine = true;
}
// always do this
swe.WriteLine(line);
count++;
}
if (length <= 2033)
{
sw.WriteLine(line);
}
line = sr.ReadLine();
}
using (StreamReader sr = new StreamReader(inputFile))
{
using (StreamWriter sw = new StreamWriter(Dts.Connections["CE802CleanInput"].ConnectionString))
{
using (StreamWriter swe = new StreamWriter(Dts.Connections["CE802PreValidationErrors"].ConnectionString));
{
swe.WriteLine("Some records have been rejected at the pre validation phase.");
swe.WriteLine("Those records will not be included in the process.");
swe.WriteLine("Please review the records below, fix and re submit if applicable.");
swe.WriteLine("Input file: " + Dts.Connections["CE802Input"].ConnectionString.ToString());
while (line = sr.ReadLine() != null)
{
int length = line.Length;
if (length > 2033)
{
swe.WriteLine();
swe.WriteLine(line);
count++;
}
else
{
sw.WriteLine(line);
}
}
}
}
}
是的!!!谢谢:-)
bool firstError = true;
while (line != null)
{
int length = line.Length;
if (length > 2033)
{
if (firstError)
{
firstError = false;
swe.WriteLine("Some records have been rejected at the pre validation phase.");
// etc. etc.