C#I/O基本操作(代码)

using System;
using System.IO;

namespace Class1
{
    class Program
    {
        static void Main(string[] args)
        {
            //写文件操作
            string path = "d:\\test.txt";

            string[] data = new string[2];
            data[0] = "a";
            data[1] = "b";

            System.IO.File.WriteAllLines(path, data);

            //读文件操作
            //1.判断文件是否存在
            //2.读
            if (System.IO.File.Exists(path))
            {
                string[] temp = new string[2];
                temp = System.IO.File.ReadAllLines(path);
                Console.WriteLine(temp[0]);
                Console.WriteLine(temp[1]);
            }

            //删除文件操作
            //1.判断文件是否存在
            //2.删除
            //if (System.IO.File.Exists(path))
            //{
            //    System.IO.File.Delete(path);
            //}

            //二进制读写操作
            //1.创建对应的流
            //2.创建对应的二进制读写器
            //3.关闭读写器
            //4.关闭流
            string path1 = "d:\\test1.txt";
            string[] data1 = new string[2];
            data1[0] = "c";
            data1[1] = "d";
            //二进制写入
            FileStream fs = new FileStream(path1, FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(data1[0]);
            bw.Write(data1[1]);
            bw.Close();
            fs.Close();
            //二进制读取
            if(System.IO.File.Exists(path))
            {
                fs = new FileStream(path1, FileMode.Open);
                BinaryReader br = new BinaryReader(fs);
                Console.WriteLine(br.ReadString());
                Console.WriteLine(br.ReadString());
                br.Close();
                fs.Close();
            }
            
            Console.ReadKey();
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值