C # common file operations text file binary file read/write operations

Source: Internet
Author: User

[Csharp] using System; using System. collections. generic; using System. linq; using System. text; using System. IO; namespace text {class Program {static void Main (string [] args) {FileStream fs = new FileStream ("test.txt", FileMode. openOrCreate); BinaryWriter bw = new BinaryWriter (fs); bw. write (1); bw. write (2); bw. write (3); bw. close (); fs. close (); Console. readKey (); Console. writeLine (); FileStream fs2 = new FileStream ("test.txt", FileMode. open); BinaryReader br = new BinaryReader (fs2); int a = br. readInt32 (); int B = br. readInt32 (); int c = br. readInt32 (); Console. writeLine (a); Console. writeLine (B); Console. writeLine (c); br. close (); fs2.Close (); Console. readKey (); FileStream fs3 = new FileStream ("test2.txt", FileMode. create); StreamWriter sw = new StreamWriter (fs3); sw. writeLine ("test from watkins"); sw. writeLine ("235"); sw. writeLine (5); sw. writeLine ("hah"); sw. close (); fs3.Close (); Console. readKey (); Console. writeLine (); FileStream fs4 = new FileStream ("test2.txt", FileMode. open); StreamReader sr = new StreamReader (fs4); string s1 = sr. readLine (); string s2 = sr. readLine (); string s3 = sr. readLine (); string s4 = sr. readLine (); Console. writeLine (s1); Console. writeLine (s2); Console. writeLine (s3); Console. writeLine (s4); Console. readKey () ;}} using System; using System. collections. generic; using System. linq; using System. text; using System. IO; namespace text {class Program {static void Main (string [] args) {FileStream fs = new FileStream ("test.txt", FileMode. openOrCreate); BinaryWriter bw = new BinaryWriter (fs); bw. write (1); bw. write (2); bw. write (3); bw. close (); fs. close (); Console. readKey (); Console. writeLine (); FileStream fs2 = new FileStream ("test.txt", FileMode. open); BinaryReader br = new BinaryReader (fs2); int a = br. readInt32 (); int B = br. readInt32 (); int c = br. readInt32 (); Console. writeLine (a); Console. writeLine (B); Console. writeLine (c); br. close (); fs2.Close (); Console. readKey (); FileStream fs3 = new FileStream ("test2.txt", FileMode. create); StreamWriter sw = new StreamWriter (fs3); sw. writeLine ("test from watkins"); sw. writeLine ("235"); sw. writeLine (5); sw. writeLine ("hah"); sw. close (); fs3.Close (); Console. readKey (); Console. writeLine (); FileStream fs4 = new FileStream ("test2.txt", FileMode. open); StreamReader sr = new StreamReader (fs4); string s1 = sr. readLine (); string s2 = sr. readLine (); string s3 = sr. readLine (); string s4 = sr. readLine (); Console. writeLine (s1); Console. writeLine (s2); Console. writeLine (s3); Console. writeLine (s4); Console. readKey ();}}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.