C# 常用文件操作 文本文件 二进制文件 读写操作

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();
        }
    }
}


你可能感兴趣的:(C# 常用文件操作 文本文件 二进制文件 读写操作)