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);
    
  • 相关阅读:
    ASP.NET Forms身份认证
    .net中调用存储过程的示例 带有输出参数
    jquery ajax .net使用
    存储过程实现批量删除
    相关分页Select语句(扩展)
    css中对于文字溢出时的控制
    精打细算过日子 车辆养用如何能更省钱
    2013年最新交通法规全文 具体扣分细节全面解读
    wxpython学习笔记(一)
    wxpython学习笔记(一)
  • 原文地址:https://www.cnblogs.com/shijun/p/2875236.html
Copyright © 2011-2022 走看看