欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# 流介绍 (原发布 csdn 2017-09-15 23:37:52)

程序员文章站 2022-06-29 09:48:31
1、FileStream FileStream 详细介绍参考 "msdn" 写数据: c using (FileStream fs = new FileStream("File.FileStream", FileMode.Create, FileAccess.Write)) { for (int i ......

1、filestream

filestream 详细介绍参考

写数据:
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(将二进制中的基元类型写入流并支持用特定的编码写入字符串。) 详细介绍参考

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 (用特定的编码将基元数据类型读作二进制值。)详细介绍参考

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 详细介绍参考

 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 详细介绍参考

 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
}