class StreamReadWrite
{
public void ReadFile()//文本
{
FileStream fs = new FileStream("C:\\MyFile.txt", FileMode.Open, FileAccess.Read,FileShare.Read);
StreamReader sr = new StreamReader(fs);
sr.BaseStream.Seek(0, SeekOrigin.Begin);
string str = sr.ReadLine();
while (str != null)
{
Console.WriteLine(str);
str = sr.ReadLine();
}
sr.Close();
fs.Close();
}
public void WriteFile()
{
FileStream fs = new FileStream(@"C:\MyFile.txt", FileMode.Append, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
Console.WriteLine("请输入一个字符串");
string str = Console.ReadLine();
sw.WriteLine();
sw.WriteLine(str);
int x = 10;
sw.Write(x);
sw.Flush();
sw.Close();
fs.Close();
}
}