Class in C #

xiaoxiao2021-03-06  37

Using system; using system.text; usingspace consoleapplication1 {///

/// readomreadfile summary description. /// public class ReadomReadFile {static FileStream fs = null; static StreamWriter sw = null; static StreamReader sr = null; static MemoryStream ms = null; static BufferedStream bs = null; static StringWriter strW = null; static StringReader strR = NULL;

STATIC BYTE [] ba = null; static string str = "";

// file positioning static void frombegin () {try {fs = new filestream ("d: //test.txt", filemode.open; if (fs.canseek) {fs.seek (3, seekorigin.begin); INT i = fs.readbyte (); system.console.writeline ("4 bytes from begin is: {0}", (char) i);}} finally {fs.close ();}} // file flow public static void FileS () {ba = new byte [] {76,101,116,32,116,104,101,114,101,32,98,101,32,108,105,103,104,116}; fs = new FileStream ( "d: //test.txt", FileMode.OpenOrCreate); fs.Write (ba, 0, ba.lend (fs.close (); fs = new filestream ("d: //test.txt", filemode.open; int i; if (fs.canread) {for (i = 0; i = fs.readbyte ())! = -1; i ) {STR = (char) i;} fs.flush (); console.writeline (STR);}} // Positioning method public static void seekmethod () {BA = new byte [] {97, 112, 112, 108, 101, 115, 97, 117, 99, 101}; fs = new filestream ("d: //test.txt", filemode.open; fs.write (ba, 0, ba.length); console. WriteLine ("Lentral: {0}, Position: {1}", fs.length, fs.position; IF (fs.canread) {fs.seek (13, seekorigin.begin); console.writeline ("Position: {0}", fs.position); fs.write (ba, 0, ba.length);} STR = ""; Fs.seek; for (int i = 0; (i = fs.readby ())! = - 1; i ) {STR = (char) i;} console. WriteLine (STR);} static void seekene () {ba = new byte [] {32, 97, 110, 100, 32, 112, 101, 97, 114, 115}; fs = new filestream ("d: //test.txt", filemode.append, fileaccess.write ); Fs.write (ba, 0, ba.length); fs.close ();

FS = New fileStream (@ "d: /test.txt", filemode.open); str = ""; // set the length fs.setlength (fs.length -5); for (int i = 0; i = fs.readbyte ())! = - 1; i ) {STR = (char) i;} console.writeline (STR); fs.close ();} // Text stream read and write public static void Readwrite () {fs = new filestream ("D: //test.txt", filemode.openorcreate); SW = New StreamWriter (FS); SW.WRITELINE ("Hello World"); sw.close (); fs = New filestream ("D: //test.txt", filemode.open; // encoding method // streamreader r = new streamreader (fs, system.text.encoding.biGendianunicode); // streamreader r = new StreamReader (fs System.Text.Encoding.ascii); // streamreader r = new streamreader (fs, system.text.encoding.unicode); // This one can display the word stream // streamreader r = new StreamReader (fs, system) .Text.Encoding.utf7) // This one can display normal display SR = New StreamReader (fs, system.text.encoding.utf8); str = ""

While ((str = sr.readline ())! = null) {console.writeline (STR);} sr.close ();} // Memorystream () {ms = new memoryStream (64); Console.writeline ("LENTH: {0} / tposition: {1} / tcapacity: {2}", ms.Length, ms.position, ms.capacity; console.writeline ("Contents:"); for (int i = 0; i <64; i ) {ms.writebyte ((byte) i);} console.writeline ("LENTH: {0} / tposition: {1} / tcapacity: {2}", Ms.Length, Ms.Position, Ms.capacity; console.writeline ("Contents:"); Byte [] ba = ms.getBuffer (); foreach (byte b in ba) {try {// Note -3 console.write (" {0, -3} ", b);} catch (exception e) {system.console.writeline (E.MESSAGE);} finally {// m.close ();}} // stream has reached the maximum capacity, Add something to it, change the capacity size Str = "foo"; for (int i = 0; i <3; i ) {ms.writebyte ((byte) str [i]);} console.writeline ("LENTH: {0} / tposition: {1} / tcapacity: {2}", ms.length, ms.position, ms.capacity; // Write this memory flow FS = New FileStream "D: / Test.txt", FileMode.Openorc Reate, fileaccess.write; ms.writeto (fs); ms.close ();} // bufferedstream: This is the same as the memory stream PUBLIC Static Void BufStream () {fs = new filestream ("D: / TEST .txt ", filemode.openorcreate, fileaccess.readwrite; bs = new bufferedstream (fs); console.writeline (" LENTH: {0} / tposition: {1} ", BS.LENGTH, BS.POSITION; console. WriteLine ("/ nContent:"); ba = new byte [bs.length]; bs.position = 0; bs.read (ba, 0, (int) bs.length); foreach (Byte B IN ba) {Console .Write ("{0, -5}", b);} str = "foo"; for (int i = 0; i <3; i

) {Bs.writebyte ((byte) str [i]);} console.writeline ("LENTH: {0} / tposition: {1}", bs.length, bs.position); for (int i = 0; I <(256-67) 1; i ) {bs.writebyte ((byte) i);} console.writeline ("LENTH: {0} / tposition: {1}", bs.length, bs.position) Bs.close ();} // string reader and writer public static void stringReaderWrite () {strw = new stringWriter (); strw.writeline ("string a song of {0} pence", 6) Str = "a pocket full of rye"; strw.write (str); // newline strw.writeline (strwline); Strw.write (New StringBuilder); strw.writeline ); //Strw.close (); console.writeline (strw); // **************************************** *************** STRINGBUILDER SB = strw.getstringbuilder (); int i = sb.length; sb.Append ("the birds began to start"; sb.insert (i, "When the PIE WAS Opered / N"); Sb.Appendformat ("/ N Wasn't That a {0} to set before the king", "Dainty Dish"); console.writeline (strw); // ** *************************************************** CONSOLE.WRITE LINE ("*********************************************"); strr = new stringReader (STR = STRR. Readline (); console.writeline (STR); console.write () strr.read ()); char [] ca = new char [37]; strr.read (CA, 0, 19); console.write (CA); console.writeline (strr.readtoend ()); // **************************************** ************** Console.writeline (); stringReader r = new stringReader (Str = strr.readline (); console.writeline (str); console. Write ((char) strr.read ()); CA = New Char [37]; console.write (CA); strr.readblock (CA, 0, 37); Console.Write (CA);

Console.writeline (Str.ReadToend ()); strr.close (); strr.close ();} static void binreadwrite () {// text write fs = new filestream ("D: /Test.txt", FileMode. Openorcreate; sw = new streamwriter (fs); sw.write ("Hello World"); sw.write (123); sw.write (45.67); sw.close (); fs .Close (); // binary write fs = new filestream ("d: /test.txt", filemode.openorcreate); binarywriter bw = new binaryWriter (fs); bw.write (123); bw.write ('') BW.WRITE (45.67); // binary reading bw.basestream.position = 0; binaryreader br = new binaryreader (fs); int i = 0; while (true) {i = bw.basestream.readbyte (); IF (-1 == i) {Console.Writeline (); Break;} console.write ("{0, -4}", i);} bw.close (); sw.close (); fs.close () // read the web page public static void webpage () {str = "http://www.microsoft.com"; URI URI = New URI (STR); WebRequest Req = WebRequest.create (URI); WebResponse Resp = Req.getResponse (); stream s = Resp.getResponsestre Am (); sr = new streamreader (s); string t = sr.readToEnd (); int i = t.indexof (""); int J = T.Indexof (""); Str = T.Substring (i, j); console.writeLine ("{0}", str);} public static void main () {

FROMBEGIN (); FILES (); seekmeth (); readwrite (); memstream (); bufstream (); stringReaderwrite (); binReadwrite (); webPage ();}}}

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

New Post(0)