FileStream读写文件

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _328_使用FileStream读写文件
{
    class Program
    {
        static void Main(string[] args)
        {
            //*****************************************************************************
            //(一)使用fileStream读取文件
            ////1.创建文件流,用来操作文件
            //FileStream stream = new FileStream("TextFile1.txt",FileMode.Open);

            ////2.读取或者写入数据
            //byte[] data = new byte[1024];//数据容器
            //                             //1byte=1字节 1024byte=1kb 1024kb=1MB 1024MB=1GB 1024GB=1T
            //while(true)
            //{
            //    int length = stream.Read(data, 0, data.Length);

            //    if(length==0)
            //    {
            //        Console.WriteLine("读取结束");
            //        break;
            //    }
            //    for (int i = 0; i < length; i++)
            //    {
            //        Console.Write(data[i] + " ");
            //    }

            //}
            //*****************************************************************************

            //*****************************************************************************
            ///(二)使用filestream完成文件复制(图片,二进制文件)
            ///43kb=42*1024byte
            FileStream readStream = new FileStream("111.jpg", FileMode.Open);

            FileStream writeStream = new FileStream("111Copy.jpg", FileMode.Create);

            byte[] data = new byte[1024];//数据容器
            while(true)
            {
                int length= readStream.Read(data, 0, data.Length);
                if(length==0)
                {
                    Console.WriteLine("读取结束");
                    break;
                }
                else
                {
                    writeStream.Write(data, 0, length);
                }
            }

            writeStream.Close();
            readStream.Close();
            //*****************************************************************************
            Console.ReadKey();

        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王大匣

你的鼓励是我创作最大动力,谢谢

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值