文件的插入与修改
Code
/// <summary>
/// IOStreamManager 的摘要说明。
/// </summary>
public class IOStreamManager
{
public static void ReadFile(string patch)
{
FileStream fs = new FileStream(patch,FileMode.Open,FileAccess.ReadWrite);
StreamReader sr = new StreamReader(fs);
string line = sr.ReadLine();
int curLine = 0;
while(line!=null)
{
Console.WriteLine("第"+(++curLine).ToString()+"行: "+line);
line = sr.ReadLine();
}
sr.Close();
fs.Close();
}
public static void EditFile(int curLine,string newLineValue,string patch)
{
FileStream fs = new FileStream(patch,FileMode.Open,FileAccess.Read);
StreamReader sr = new StreamReader(fs);
string line = sr.ReadLine();
StringBuilder sb = new StringBuilder();
for(int i=1;line!=null;i++)
{
sb.Append(line+"\r\n");
if(i!= curLine-1)
line = sr.ReadLine();
else
{
sr.ReadLine();
line = newLineValue;
}
}
sr.Close();
fs.Close();
FileStream fs1 = new FileStream(patch,FileMode.Open,FileAccess.Write);
StreamWriter sw = new StreamWriter(fs1);
sw.Write(sb.ToString());
sw.Close();
fs.Close();
}
}
/// <summary>
/// IOStreamManager 的摘要说明。
/// </summary>
public class IOStreamManager
{
public static void ReadFile(string patch)
{
FileStream fs = new FileStream(patch,FileMode.Open,FileAccess.ReadWrite);
StreamReader sr = new StreamReader(fs);
string line = sr.ReadLine();
int curLine = 0;
while(line!=null)
{
Console.WriteLine("第"+(++curLine).ToString()+"行: "+line);
line = sr.ReadLine();
}
sr.Close();
fs.Close();
}
public static void EditFile(int curLine,string newLineValue,string patch)
{
FileStream fs = new FileStream(patch,FileMode.Open,FileAccess.Read);
StreamReader sr = new StreamReader(fs);
string line = sr.ReadLine();
StringBuilder sb = new StringBuilder();
for(int i=1;line!=null;i++)
{
sb.Append(line+"\r\n");
if(i!= curLine-1)
line = sr.ReadLine();
else
{
sr.ReadLine();
line = newLineValue;
}
}
sr.Close();
fs.Close();
FileStream fs1 = new FileStream(patch,FileMode.Open,FileAccess.Write);
StreamWriter sw = new StreamWriter(fs1);
sw.Write(sb.ToString());
sw.Close();
fs.Close();
}
}