C # study notes six (stream, asynchronous data read, httpwebRequest)

zhaozj2021-02-16  56

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 (); 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 (buffout)} buffout.flush (); buffin.close (); buffout.close (); // streamread, Work with text file fileinfo thereourcefile = 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 end () 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 ) {c Onsole.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);

转载请注明原文地址:https://www.9cbs.com/read-24450.html

New Post(0)