zoukankan      html  css  js  c++  java
  • C# read file to bytes,File.ReadAllFiles,File.Open(),BinaryReader

    using System;
    using System.Text;
    using System.IO;
    
    namespace ConsoleApplication15
    {
        class Program
        {
            static void Main(string[] args)
            {
                string fileName = @"....Imageslj.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#中调用Outlook API 发起会议
    Log4Net配置
    web端调用Webapi获取Excel表格
    表格导出之Aspose.Cells
    验证输入框
    把新建的对象所有属性变成默认值
    省市区三级联动
    全局异常处理
    HttpHttpServletRequest / Reponse
    热部署
  • 原文地址:https://www.cnblogs.com/Fred1987/p/12010150.html
Copyright © 2011-2022 走看看