C#学习笔记之六(stream, Asynchronous data read, HttpWebRequest)

类别:.NET开发 点击:0 评论:0 推荐:

Streaming Data:

 // Explorer Directory
 string dir = @"C:\Winnt";
 DirectoryInfo dirInfo = new DirectoryInfo(dir); 
 
 //read and write File
 Stream inputStream = File.OpenRead(@"c:\test\source\hamlet.txt");
 Stream outputStream = File.OpenWrite(@"c:\test\source\hamlet.bak");
 byte[] buff = new Byte[SizeBuff];
 int byteRead;
 
 bytesRead = inputStream.Read(buffer, 0, SizeBuff);
 while (bytesRead >0)
 {
  outputStream.Write(buffer, 0 bytesRead);
  bytesRead = inputStream.Read(buffer, 0, SizeBuff);
 }
 inputStream.Close();
 outputStream.Close();

 //buffed stream
 Stream inputStream = File.OpenRead(@"c:\test\source\hamlet.txt");
 Stream outputStream = File.OpenWrite(@"c:\test\source\hamlet.bak");
 BufferedStream buffIn = new BufferedStream(inputstream);
 BufferedStream buffOut = new BufferedStream(outputStream);
 
 byte[] buffer = new Byte[SizeBuff]; 
 int bytesRead;
 while ((bytesRead = buffIn.Read(buffer.0,SizeBuff))> 0)
 {
  buffOut.Write(buffer,0, butesRead)
 }
 buffOut.Flush();
 buffIn.close();
 buffOut.close();
 
 //StreamRead, work with text file
 Fileinfo theSourceFile = new FileInfo(
  @"c:\test\source\hamlet.txt");
 StreamReader reader = theSourceFile.OpenText();
 StreamWriter writer = new StreamWriter(@"c:\hamlet.bak", false);
 string text;
 do {
  text = reader.ReadLine();
  writer.WriteLine(text);
 } wheil (text != null);

Asynchronous:
//Steps:
//1. Begin Read(), Give Buffer,CallBack method
//2. When buffer is filled, call CallBack fucntion and send back AsyncResult token
//3. In CallBack method call EndRead() and send back got token AsyncResult
//4. Data is available
 public class AsyncTest{
 private Stream inStream;
 private AsyncCallback callBackMethod;
 private byte[] buffer;
 
 AsyncTest()
 {
  inStream = File.OpenRead(@"c:\a.txt");
  buffer = new byte[BuffSize];
  callBackMethod = new AsyncCallback(this.OnCompletedRead);
  
  
 }
 void OnCompletedRead(IAsyncResult asyncResult)
 {
  int bytesRead = inStream.EndRead(asyncResult);
  if (butesRead >0)
  {
   string s =
    Encoding.ASCII.GetString(buffer, 0, butesRead);
   inStream.BeginRead(buffer, 0, buffer.Length,
    callBackMethod, null);
  }
 }
 public Main()
 {
  AsyncTest tester = new AsyncTest();
  inStream.BeginRead(
   buffer,
   0, //offset
   buffer.Length,
   callBackMethod, //call back delegate
   null );  //local state object
    
  for(long i = 0; i< 500000, i++)
  {
   Console.WriteLine("i: {0}", i);
  }
  
 }
 } 
HttpWebRequest:
 //1. call WebRequest.Create(), static method, will return WebRequest
 //2. call WebRequest.GetResponse(), instance method, return a WebResponse
 //3. call WebResponse.GetResponseStream, instance method, return a stream reader
 //4. call streamReader.ReadToEnd() return a string with the entire stream
 using System.Net;
 using System.Sockets;
 HttpWebRequest webRequest = (HttpWebRequest) WebRequest.Create("http://a.com");
 HttpWebResponse webResponse = (HttpWebResponse) webRequest.GetResponse();
 StreamReader streamReader = new StreamReade(
  webResponse.GetResponseStream(), Encoding.ASCII);
  try
  {
   string outputString;
   outputString = streamReader.ReadToEnd();
   Console.WriteLine(outputStirng);
  }  
  catch {}
 }

本文地址:http://com.8s8s.com/it/it45068.htm