首页 > Windows开发 > 详细

C# read file to bytes,File.ReadAllFiles,File.Open(),BinaryReader

时间:2019-12-09 14:30:23      阅读:62      评论:0      收藏:0      [点我收藏+]
using System;
using System.Text;
using System.IO;

namespace ConsoleApplication15
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = @"..\..\Images\lj.jpg";
            FileStreamReadToBytes(fileName);
            ReadFileToBinaryBytes(fileName);
            FileReadAllBytesDemo(fileName);
            Console.ReadLine();
        }

        static void FileStreamReadToBytes(string fileName)
        {
            if(!File.Exists(fileName))
            {
                return;
            }

            byte[] bytesArr = null;
            using (FileStream fs = new FileStream(fileName, FileMode.Open))
            {
                bytesArr = new byte[fs.Length];
                fs.Write(bytesArr, 0, bytesArr.Length);
                Console.WriteLine(bytesArr.Length);
            }
        }

        static void FileReadAllBytesDemo(string fileName)
        {
            byte[] readBytes = System.IO.File.ReadAllBytes(fileName);
            Console.WriteLine(readBytes.Length);
        }

        static void ReadFileToBinaryBytes(string fileName)
        {
            if (!File.Exists(fileName))
            {
                return;
            }

            byte[] bytesArr = null;
            using (FileStream fs = new FileStream(fileName, FileMode.Open))
            {
                using (BinaryReader binReader = new BinaryReader(fs,Encoding.UTF8))
                {
                    bytesArr = binReader.ReadBytes((int)fs.Length);
                    Console.WriteLine(bytesArr.Length);
                }                    
            }
        }
    }    
}

 

C# read file to bytes,File.ReadAllFiles,File.Open(),BinaryReader

原文:https://www.cnblogs.com/Fred1987/p/12010150.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!