zoukankan      html  css  js  c++  java
  • 文件创建及读取的方法

    以前创建用

          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();
            }


     

  • 相关阅读:
    django-3-模板变量,过滤器,静态文件的引用
    django-2-路由配置及渲染方式
    用pycharm运行django项目
    django-1-框架介绍
    django-6-数据库配置及模型创建,激活(django模型系统1)
    用Sklearn实现聚类算法并用散点图展现效果
    方差、标准差、协方差、协方差相关系数
    ARIMa--时间序列模型
    人工智能--第二天--KNN算法实现
    人工智能--第二天--KNN算法
  • 原文地址:https://www.cnblogs.com/wf225/p/571768.html
Copyright © 2011-2022 走看看