标签:
文件流读写
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace _09文件流 { class Program { static void Main(string[] args) { //string msg = "飞流直下三千尺"; ////字符串转字节数组 //byte[] buffer = System.Text.Encoding.UTF8.GetBytes(msg); ////字节数组转字符串 //string str= System.Text.Encoding.UTF8.GetString(buffer); //把字符串写入到文件中,以流的方式写内容 //using ( FileStream fs = new FileStream("1.txt", FileMode.Create, FileAccess.Write)) //{ // string msg = "文能提笔控萝莉"; // byte[] buffer = System.Text.Encoding.UTF8.GetBytes(msg); // fs.Write(buffer, 0, buffer.Length); //}//Console.ReadKey(); //fs.Close();//关闭流 //fs.Flush();//清除缓冲区 //fs.Dispose();//释放占用的资源 (这三个必须一起写,用using方式就不用这三个释放资源了) //以流的方式读数据 using (FileStream fs = new FileStream("1.txt", FileMode.Open, FileAccess.Read)) { byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); string msg = System.Text.Encoding.UTF8.GetString(buffer); Console.WriteLine(msg); } Console.ReadKey(); } } }
大文件移动
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace _10大文件移动 { class Program { static void Main(string[] args) { //读的流 using (FileStream fsRead=new FileStream(@"G:\视频\海盗.mkv",FileMode.Open, FileAccess.Read)) { //写的流 using (FileStream fsWrite=new FileStream(@"G:\电影\海盗.mkv", FileMode.Create, FileAccess.Write)) { //每次读取的大小是5M byte[]buffer=new byte[1024*1024*5]; //实际(真正读取到的大小) int r= fsRead.Read(buffer, 0, buffer.Length); while (r>0) { //写入 fsWrite.Write(buffer, 0, r); Console.WriteLine("**"); //再读取 r = fsRead.Read(buffer, 0, buffer.Length); } } } Console.WriteLine("ok了"); Console.ReadKey(); } } }
另一种方式的读写
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace _11另一种方式的读和写 { class Program { static void Main(string[] args) { #region 读取数据 //using (StreamReader reader = new StreamReader("1.txt",Encoding.Default)) //{ //只读取了一行 //string msg= reader.ReadLine(); //string msg; //要循环读取 //while ((msg=reader.ReadLine())!=null) //{ // Console.WriteLine(msg); //} //一直读取到流的末尾 // string msg= reader.ReadToEnd(); // Console.WriteLine(msg); //while (!reader.EndOfStream) //{ // Console.WriteLine(reader.ReadLine()); //} // } #endregion #region 写入数据 //using (StreamWriter write=new StreamWriter("one.txt")) //{ // write.Write("原来这也可以啊"); //} #endregion // Console.ReadKey(); } } }
文件流读写、大文件移动 FileStream StreamWriter
标签:
原文地址:http://www.cnblogs.com/xiaoshi657/p/5243647.html