using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace 读写文件
{
class Program
{
static void Main(string[] args)
{
FileStream Myfile = new FileStream("Myfilename.txt", FileMode.Create, FileAccess.ReadWrite);
Console.WriteLine("****************读取文件属性*********************");
FileInfo Finfo = new FileInfo("Myfilename.txt");
Console.WriteLine("文件名 :{0}", Finfo.Name);
Console.WriteLine("文件名(含路径):{0}", Finfo.FullName);
Console.WriteLine("文件大小(byte):{0}", Finfo.Length);
Console.WriteLine("文件创建日期 :{0}", Finfo.CreationTime);
StreamWriter StrWrite = new StreamWriter(Myfile);
StrWrite.WriteLine("Hello word");
StrWrite.WriteLine("Write line stream");
StrWrite.Flush();
StreamReader StrRead = new StreamReader(Myfile);
StrRead.BaseStream.Seek(0, SeekOrigin.Begin);
Console.WriteLine("****************以文本方式读文件*********************");
string s1;
while ((s1 = StrRead.ReadLine()) != null)
{
Console.WriteLine(s1);
}
// 以二进制方式读文件
Console.WriteLine();
BinaryReader Breader = new BinaryReader(Myfile);
Breader.BaseStream.Seek(0, SeekOrigin.Begin);
Console.WriteLine("****************以二进制方式读文件*********************");
byte b1;
while(Breader.PeekChar ()>-1)
{
b1 = Breader.ReadByte();
// 13为"\n",表示回车;10为"\r",表示换行
if (b1 != 13 && b1 != 10)
{
Console.Write("{0}", b1.ToString());
Console.Write(".");
}
else
{
Console.WriteLine();
}
}
Console.WriteLine("\n");
Myfile.Close();
StrWrite.Close();
StrRead.Close();
Breader.Close();
}
}
}
为什么加了这段就错了?是不是因为读取文件时,就已经把写入文件关闭了.