zoukankan      html  css  js  c++  java
  • 文件与流相关code

    FileStream方式写入文件

    FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
    fs.Write(fileData, 0, fileData.Length); //写入流  
    fs.Flush(); //清空缓冲区 
    fs.Close(); //关闭流
    fs.Dispose();

    BinaryWriter方式写文件

    FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
    using (BinaryWriter bw = new BinaryWriter(fs))
    {
        bw.Write(fileData); //写入流                    
        bw.Flush();//清空缓冲区                    
        bw.Close(); //关闭流
    }
    fs.Close();
    fs.Dispose();
    
    FileStream fs1 = new FileStream(@"x1.doc", FileMode.Open);
    FileStream fs2 = new FileStream(@"x2.doc", FileMode.Create);
    byte[] data = new byte[1024];
    //创建两个缓冲流,与两个文件流相关联
    BufferedStream bs1 = new BufferedStream(fs1);
    BufferedStream bs2 = new BufferedStream(fs2);
    while (fs1.Read(data, 0, data.Length) > 0)
    {
        fs2.Write(data, 0, data.Length);
        fs2.Flush();
    }
    fs1.Close();
    fs2.Close();
    
    FileStream fs = new FileStream(op.FileName, FileMode.Open);
    //把文件读取到字节数组
    byte[] data = new byte[fs.Length];
    fs.Read(data, 0, data.Length);
    fs.Close();
    
    Stream s = fileUpload.PostedFile.InputStream;
    //文件临时储存Byte数组
    Byte[] fileData = new Byte[fileUpload.PostedFile.ContentLength];
    s.Read(fileData, 0, fileUpload.PostedFile.ContentLength);
    s.Close();
    FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write); 
    StreamWriter sw = new StreamWriter(fs);
    sw.Write(this.textBox1.Text);
    sw.Flush();//清空缓冲区
    sw.Close();//关闭流
    fs.Close();  
    
    //获得字节数组  
    byte[] data = new System.Text.UTF8Encoding().GetBytes(this.textBox1.Text);
    
  • 相关阅读:
    (二)vue.js axios封装(参考)
    (一)vue.js 项目搭建(参考)
    Centos7 Mysql配置安装
    自我学习及提高
    (一)Angular2 项目搭建教程(参考)
    WordPress学习网站
    Excel A表与B表对比 并将A表的id写到B表中
    C# 递归文件夹 文件名 路径
    C# 递归生成树(无限级)
    Windows API Code Pack 获取文件缩略图
  • 原文地址:https://www.cnblogs.com/shijun/p/2875236.html
Copyright © 2011-2022 走看看