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

2021-01-25 18:14

阅读:746

标签:using   length   adl   ram   read   files   line   reader   binary   

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

标签:using   length   adl   ram   read   files   line   reader   binary   

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


评论


亲,登录后才可以留言!