嗨,当用户检查不需要架构时,我有一个包含表模式和数据的文本文件,然后我需要删除模式并保留数据.我正在使用StreamReader读取文件并检查一个条件,它应该删除文件中的所有行,直到它满足我的条件.如果我正在检查,请说
using (StreamReader tsr = new StreamReader(targetFilePath))
{
do
{
string textLine = tsr.ReadLine() + "\r\n";
{
if (textLine.StartsWith("INSERT INTO"))
{
// It should leave these lines
// and no need to delete lines
}
else
{
// it should delete the lines
}
}
}
while (tsr.Peek() != -1);
tsr.Close();
Run Code Online (Sandbox Code Playgroud)
请建议我如何删除行,并注意如果textline找到"InsertInto",它不应该从那里删除任何内容.
使用第二个文件,只放置所需的行,并在流程结束时删除原始文件并将新文件重命名为目标文件.
using (StreamReader tsr = new StreamReader(targetFilePath))
{
using (StreamWriter tsw = File.CreateText(targetFilePath+"_temp"))
{
string currentLine;
while((currentLine = tsr.ReadLine()) != null)
{
if(currentLine.StartsWith("A long time ago, in a far far away galaxy ..."))
{
tsw.WriteLine(currentLine);
}
}
}
}
File.Delete(targetFilePath);
File.Move(targetFilePath+"_temp",targetFilePath);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
151 次 |
| 最近记录: |