.NET中的IO操作之文件流
读操作 //1.创建文件流 FileStream fsRead =new FileStream("1.txt",FileMode.Open); //2.创建缓冲区,正常情况下,是不会直接等于文件大小的。这里只有读,所以就这么干了。 byte[] bytes =new byte[fsRead.Length]; //3.开始读取, 返回值是读取到的长度。 int r =fsRead.Read(bytes,0,bytes.Lenght); //4.关闭释放流 fsRead.Close(); fsRead.Dispose(); 写操作 //1.创建写入的文件流 FileStream fsWrite fsWrite =new FileStream(@"xxx",FileMode.OpenOrCreate); //2.创建缓冲区 String msg ="HelloWorld"; byte[] bytes =Enconding.UTF8.GetBytes(msg); //3.开始写入 fsWrite.Write(bytes,0,bytes.Length); //4.关闭 fsWrite.Close(); fsWrite.Dispose(); byte数组与string之间的转换 /*在文件流写入的时候,经常需要string 和 byte数组之间的转换。 这里简单的描述一下,这方面的做法。 */