Csharp/C Sharp by API/System.IO/BufferedStream

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

BufferedStream.Position

<source lang="csharp"> using System; using System.IO;

   public class BufStrm
   {
       static public void Main ()
       {
           FileStream strm;
           strm = new FileStream ("test.txt",FileMode.OpenOrCreate,FileAccess.Write);
           strm.SetLength (0);
           BufferedStream bstrm = new BufferedStream (strm);
           string str = "this is a test";
           byte [] b;
           StringToByte (out b, str);
           bstrm.Write (b, 0, b.Length);
           long Pos = bstrm.Position;
           Console.WriteLine ("The buffered stream position is "+ bstrm.Position);
           Console.WriteLine ("The underlying stream position is "+ strm.Position);
           str = "the quick red fox jumps over the lazy brown dog.\r\n";
           StringToByte (out b, str);
           bstrm.Write (b, 0, b.Length);
           Console.WriteLine ("\r\nThe buffered stream position is " +bstrm.Position);
           Console.WriteLine ("The underlying stream position is still "+ strm.Position);
           bstrm.Seek (Pos, SeekOrigin.Begin);
           b[0] = (byte) "T";
           bstrm.Write (b, 0, 1);
           bstrm.Flush ();
           bstrm.Close ();
           strm.Close ();
       }
       static void StringToByte (out byte [] b, string str)
       {
           b = new byte [str.Length];
           for (int x = 0; x < str.Length; ++x)
           {
               b[x] = (byte) str [x];
           }
       }
   }
  
   
 </source>


BufferedStream.Read

<source lang="csharp">

using System; using System.IO; class BufStreamApp {

   static void Main(string[] args)
   {
       FileStream fs = new FileStream("Hoo.txt",FileMode.Create, FileAccess.ReadWrite);
       BufferedStream bs = new BufferedStream(fs);
       Console.WriteLine("Length: {0}\tPosition: {1}",bs.Length, bs.Position);
  
       for (int i = 0; i < 64; i++)
       {
           bs.WriteByte((byte)i);
       }
       Console.WriteLine("Length: {0}\tPosition: {1}", bs.Length, bs.Position);
       byte[] ba = new byte[bs.Length];
       bs.Position = 0;
       bs.Read(ba, 0, (int)bs.Length);
       foreach (byte b in ba)
       {
           Console.Write("{0,-3}", b);
       }
  
       string s = "Foo";
       for (int i = 0; i < 3; i++)
       {
           bs.WriteByte((byte)s[i]);
       }
       Console.WriteLine("\nLength: {0}\tPosition: {1}\t",bs.Length, bs.Position);
  
       for (int i = 0; i < (256-67)+1; i++)
       {
           bs.WriteByte((byte)i);
       }
       Console.WriteLine("\nLength: {0}\tPosition: {1}\t", bs.Length, bs.Position);
  
       bs.Close();
   }

}

 </source>


BufferedStream.Write

<source lang="csharp"> using System; using System.Windows.Forms; using System.IO; public class MainClass {

 public static void Main() 
 {
   OpenFileDialog dlgOpen = new OpenFileDialog();
   dlgOpen.Title="Select file to back up";
   if (dlgOpen.ShowDialog() == DialogResult.OK)
   {
     FileStream inStream = File.OpenRead(dlgOpen.FileName);
     FileStream outStream = File.OpenWrite(dlgOpen.FileName + ".bak");
     BufferedStream bufInStream = new BufferedStream(inStream);
     BufferedStream bufOutStream = new BufferedStream(outStream);
     byte[] buf = new byte[4096];
     int bytesRead;
     while ((bytesRead = bufInStream.Read(buf, 0, 4096)) > 0)
       bufOutStream.Write(buf, 0, bytesRead);
     bufOutStream.Flush();
     bufOutStream.Close();
     bufInStream.Close();
     outStream.Close();
     inStream.Close();
   }
 }

}

 </source>


BufferedStream.WriteByte

<source lang="csharp">

using System; using System.IO; class BufStreamApp {

   static void Main(string[] args)
   {
       FileStream fs = new FileStream("Hoo.txt",FileMode.Create, FileAccess.ReadWrite);
       BufferedStream bs = new BufferedStream(fs);
       Console.WriteLine("Length: {0}\tPosition: {1}",bs.Length, bs.Position);
  
       for (int i = 0; i < 64; i++)
       {
           bs.WriteByte((byte)i);
       }
       Console.WriteLine("Length: {0}\tPosition: {1}", bs.Length, bs.Position);
       byte[] ba = new byte[bs.Length];
       bs.Position = 0;
       bs.Read(ba, 0, (int)bs.Length);
       foreach (byte b in ba)
       {
           Console.Write("{0,-3}", b);
       }
  
       string s = "Foo";
       for (int i = 0; i < 3; i++)
       {
           bs.WriteByte((byte)s[i]);
       }
       Console.WriteLine("\nLength: {0}\tPosition: {1}\t",bs.Length, bs.Position);
  
       for (int i = 0; i < (256-67)+1; i++)
       {
           bs.WriteByte((byte)i);
       }
       Console.WriteLine("\nLength: {0}\tPosition: {1}\t", bs.Length, bs.Position);
  
       bs.Close();
   }

}

 </source>


new BufferedStream(FileStream fs)

<source lang="csharp"> using System; using System.IO; public class MainClass {

   public static int Main(string[] args)
   {
   FileStream dumpFile = new FileStream("Dump.dat", FileMode.Create, FileAccess.ReadWrite);
   BufferedStream myFileBuffer = new BufferedStream(dumpFile);
   
   byte[] str = {127, 0x77, 0x4, 0x0, 0x0, 0x16};
   myFileBuffer.Write(str, 0, str.Length);
   // add changes to file.
   myFileBuffer.Close();  // Flushes.
   dumpFile.Close();  // Flushes.
   return 0;
   }

}


 </source>