• 文件创建及读取的方法


    以前创建用

          String filePath = HttpContext.Current.Server.MapPath(FileName);

            
    if (!System.IO.File.Exists(filePath))// 创建文件
                System.IO.File.Create(filePath);

            System.IO.StreamWriter sw 
    = new System.IO.StreamWriter(filePath, false);
            sw.WriteLine(html);
            sw.Close();

      读取用

          if (System.IO.File.Exists(filePath))
            
    {
                
    //System.IO.FileStream fs = System.IO.File.OpenRead(filePath); 
                FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); 

                StreamReader sr 
    = new StreamReader(filePath, System.Text.Encoding.UTF8);
                
    //StreamReader sr = new StreamReader(filePath);

                
    while (sr.Peek() > -1)//StreamReader.Peek()返回下一个可用字符,但不使用它 
                {
                    Response.Write(sr.ReadLine());
                }

                sr.Close(); 
                fs.Close();
            }


     如果文件不存在的话,就会有

    文件“G:\wwwRoot\wufengBS\文件处理\code.xls”正由另一进程使用,因此该进程无法访问该文件。

    的错误。

    改用流创建即可:

            if (!System.IO.File.Exists(filePath))// 创建文件
            {
                System.IO.FileStream fs 
    = System.IO.File.Create(filePath);
                fs.Close();
            }


     

  • 相关阅读:
    基于tensorflow的简单线性回归模型
    msm8909平台JEITA配置和bat-V therm表合入
    开始点滴积累
    消息队列中间件(一)介绍
    Ubuntu18 的超详细常用软件安装
    IO通信模型(三)多路复用IO
    IO通信模型(二)同步非阻塞模式NIO(NonBlocking IO)
    IO通信模型(一)同步阻塞模式BIO(Blocking IO)
    Web笔记(二)Tomcat 使用总结
    const in C/C++
  • 原文地址:https://www.cnblogs.com/wf225/p/571768.html
Copyright © 2020-2023  润新知