C# 流介绍 (原发布 csdn 2017

Stella981
• 阅读 697

1、FileStream

FileStream 详细介绍参考msdn

写数据:
using (FileStream fs = new FileStream("File.FileStream", FileMode.Create, FileAccess.Write))
{
    for (int i = 0; i < Cycles; i++)
    {
        for (int j = 0; j < Length; j++)
        {
            dis[j] = i * Length + j;
        }
        Buffer.BlockCopy(dis, 0, byData, i * readCount, readCount);
    }
    fs.Write(byData, 0, byData.Length);
}
读数据
using (FileStream fs = new FileStream("File.FileStream", FileMode.Open, FileAccess.Read))
{
    for (int i = 0; i < Cycles; i++)
    {
        fs.Seek(i * readCount, SeekOrigin.Begin);
        fs.Read(byData, 0, readCount);
        dis = new double[Length];
        Buffer.BlockCopy(byData, i * readCount, dis, 0, readCount);
    }
}

2、BinaryWriter/BinaryReader

2.1 BinaryWriter(将二进制中的基元类型写入流并支持用特定的编码写入字符串。) 详细介绍参考msdn

using (BinaryWriter bw = new BinaryWriter(File.Open("File.Binary", FileMode.Create)))
{
    byte[] data = new byte[Cycles * readCount];
    for (int i = 0; i < Cycles; i++)
    {
        for (int j = 0; j < Length; j++)
        {
            dis[j] = i * Length + j;
        }
        Buffer.BlockCopy(dis, 0, data, i * readCount, readCount);
    }
    bw.Write(data);
}

2.2 BinaryReader (用特定的编码将基元数据类型读作二进制值。)详细介绍参考msdn

using (BinaryReader wr = new BinaryReader(File.Open("File.Binary", FileMode.Open)))
{
    for (int i = 0; i < Cycles; i++)
    {
        var readData = wr.ReadBytes(readCount);
        dis = new double[Length];
        Buffer.BlockCopy(readData, 0, dis, 0, readCount);
    }
}

3、StreamWriter/StreamReader

3.1 StreamWriter 详细介绍参考msdn

 using (StreamWriter sw = new StreamWriter("File.Stream", false, Encoding.GetEncoding("utf-16")))
 {
     StringBuilder sb = new StringBuilder();
     for (int i = 0; i < Cycles; i++)
     {
         for (int j = 0; j < Length; j++)
         {
             dis[j] = i * Length + j;
             sb.AppendFormat("{0},", dis[j]);
         }
         sb.AppendFormat("\n");
     }
     sw.WriteLine(sb);
 }

3.2 StreamReader 详细介绍参考msdn

 using (StreamReader sd = new StreamReader("File.Stream", Encoding.GetEncoding("utf-16")))
 {
     for (int i = 0; i < Cycles; i++)
     {
         string[] ch = sd.ReadLine().Split(new Char[] { ',' },
                                           System.StringSplitOptions.RemoveEmptyEntries);
         for (int j = 0; j < Length; j++)
         {
             double.TryParse(ch[j], out dis[j]);
         }
     }
 }

4 完整测试代码:

class Program
{
    static void Main()
    {
        fileReadAndWrite.BinaryWriterMethod();
        fileReadAndWrite.BinaryReaderMethod();
       
        fileReadAndWrite.FileStreamWriterMethod();
        fileReadAndWrite.FileStreamReadMethod();

        fileReadAndWrite.StreamWriterMethod();        
        fileReadAndWrite.StreamReaderMethod();        
        
        Console.ReadKey(true);
    }
}


class FileReadAndWrite
{
    private const int Length = 1024;
    private const int Cycles = 64;
    private int readCount;
    private byte[] byData;
    private double[] dis;
    
    public FileReadAndWrite()
    {
        readCount = Length * sizeof(double);
        dis = new double[Length];
        byData = new byte[Cycles * Length * sizeof(double)];
    }
    
    #region BinaryWriter\BinaryReader        
    public void BinaryWriterMethod()
    {
        using (BinaryWriter bw = new BinaryWriter(File.Open("File.Binary", FileMode.Create)))
        {
            byte[] data = new byte[Cycles * readCount];
            for (int i = 0; i < Cycles; i++)
            {
                for (int j = 0; j < Length; j++)
                {
                    dis[j] = i * Length + j;
                }
                Buffer.BlockCopy(dis, 0, data, i * readCount, readCount);
            }
            bw.Write(data);
        }
    }
    
    public void BinaryReaderMethod()
    {
        using (BinaryReader wr = new BinaryReader(File.Open("File.Binary", FileMode.Open)))
        {
            for (int i = 0; i < Cycles; i++)
            {
                var readData = wr.ReadBytes(readCount);
                Buffer.BlockCopy(readData, 0, dis, 0, readCount);
            }
        }
    }    
    #endregion
        
    #region FileStream Read\Write
    public void FileStreamWriterMethod()
    {
        using (FileStream fs = new FileStream("File.FileStream", FileMode.Create,FileAccess.Write))
        {
            for (int i = 0; i < Cycles; i++)
            {
                for (int j = 0; j < Length; j++)
                {
                    dis[j] = i * Length + j;
                }
                Buffer.BlockCopy(dis, 0, byData, i * readCount, readCount);
            }
            fs.Write(byData, 0, byData.Length);
        }
    }
    
    public void FileStreamReadMethod()
    {
        using (FileStream fs = new FileStream("File.FileStream", FileMode.Open, FileAccess.Read))
        {
            for (int i = 0; i < Cycles; i++)
            {
                fs.Seek(i * readCount, SeekOrigin.Begin);
                fs.Read(byData, 0, readCount);
                Buffer.BlockCopy(byData, i * readCount, dis, 0, readCount);
            }
        }
    }
    #endregion

    #region StreamWriter\StreamReader
    public void StreamWriterMethod()
    {
        using (StreamWriter sw = new StreamWriter("File.Stream", false, 
                                                  Encoding.GetEncoding("utf-16")))
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < Cycles; i++)
            {
                for (int j = 0; j < Length; j++)
                {
                    dis[j] = i * Length + j;
                    sb.AppendFormat("{0},", dis[j]);
                }
                sb.AppendFormat("\n");
            }
            sw.WriteLine(sb);
        }
    }
    
    public void StreamReaderMethod()
    {
        using (StreamReader sd = new StreamReader("File.Stream", Encoding.GetEncoding("utf-16")))
        {
            for (int i = 0; i < Cycles; i++)
            {
                string[] ch = sd.ReadLine().Split(new Char[] { ',' },
                                                  System.StringSplitOptions.RemoveEmptyEntries);
                for (int j = 0; j < Length; j++)
                {
                    double.TryParse(ch[j], out dis[j]);
                }
            }
        }
    }
    #endregion
}
点赞
收藏
评论区
推荐文章
待兔 待兔
5个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Wesley13 Wesley13
3年前
java类封装成dll
@参考文章1(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fblog.csdn.net%2Fazure_sky_2014%2Farticle%2Fdetails%2F71915605),@参考文章2(https://www.oschina.net/action/GoToLink?u
Stella981 Stella981
3年前
Opencv中Mat矩阵相乘——点乘、dot、mul运算详解
Opencv中Mat矩阵相乘——点乘、dot、mul运算详解2016年09月02日00:00:36 \牧野(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fme.csdn.net%2Fdcrmg) 阅读数:59593
Wesley13 Wesley13
3年前
(三) 查看USB设备
查看USB设备原文更详细参考(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fblog.csdn.net%2Fencourage2011%2Farticle%2Fdetails%2F53525297)\TOC\lsusb列出
Stella981 Stella981
3年前
PyTorch之前向传播函数自动调用forward
参考:1. pytorch学习笔记(九):PyTorch结构介绍(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fblog.csdn.net%2Fu012436149%2Farticle%2Fdetails%2F70145598)2.pytorch学习笔记(七):pytorchh
Stella981 Stella981
3年前
EntityFramework GroupJoin
总而言之,GroupJoin是先Join后Group,对应的SQL也是先Join,然后通过内置LINQ操作分组.参考文档GroupJoin方法(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fmsdn.microsoft.com%2
Stella981 Stella981
3年前
ASP.NET Web.config Transformations
...参考文档WebDeploymentContentMapforVisualStudioandASP.NET(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fmsdn.microsoft.com%2Fenus%2Flibrary%2F
Stella981 Stella981
3年前
CabloyJS自带工作流引擎的文档清单
文档清单CabloyJS自带工作流引擎文档已经整理出来,欢迎大家围观、拍砖介绍介绍(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fcabloy.com%2Fzhcn%2Farticles%2Fflowintroduce.html)
Stella981 Stella981
3年前
Google地球出现“无法连接到登录服务器(错误代码:c00a0194)”解决方法
Google地球出现“无法连接到登录服务器(错误代码:c00a0194)”解决方法参考文章:(1)Google地球出现“无法连接到登录服务器(错误代码:c00a0194)”解决方法(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fwww.codeprj.com%2Fblo
Stella981 Stella981
3年前
K8S源码分析Controller Manager
ControllerManger是K8S内部的控制中心,主要介绍可以参考这篇文档:https://blog.csdn.net/huwh\_/article/details/75675761(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fblog.csdn.net%2Fhuwh_%