文件流创建文件后,还能再向固定的行插入内容吗?也就是你想在哪行插入就在哪行插入内容!
文件流创建文件后,还能再向固定的行插入内容吗?也就是你想在哪行插入就在哪行插入内容!求能加入任意行的代码....谢谢!
程序代码:static void Main(string[] args)
{
string document = File.ReadAllText(@"d:\temp\a.txt");
int index = GetIndex(3, document);
if (index == -1)
{
throw new Exception("...");
}
else
{
document = document.Insert(index, "Insert text");
}
File.WriteAllText(@"d:\temp\a.txt", document);
}
程序代码:static int GetIndex(int no, string document)
{
int lineNo = 0;
for (int i = 0; i < document.Length; i++)
{
if (document[i] == '\n')
{
lineNo++;
if (lineNo == no)
return i;
}
}
return -1;
}