zoukankan      html  css  js  c++  java
  • c# 读/写文件(各种格式)

    最简单的:
     
    --------写
     //content是要写入文本的字符串
     //(@txtPath + @"" + rid + ".txt");要被写入的TXT
      StreamWriter sw = new StreamWriter(@txtPath + @"" + rid + ".txt");
      sw.WriteLine(content);
      sw.Close();
     ---------读
     //folder被读取的文件路径
     text = File.ReadAllText(folder, System.Text.Encoding.GetEncoding("gb2312"));
    
    
     
    -------写入流
                 //将string写入文件    
                 try
                     {
                         FileStream fs = new FileStream(@"d:数据处理法规20080116HTML" + Actid.ToString() + ".html", FileMode.OpenOrCreate, FileAccess.Write);
                         StreamWriter sw = new StreamWriter(fs,System.Text.Encoding.GetEncoding("gb2312"));
                         sw.Flush();
                         sw.BaseStream.Seek(0, SeekOrigin.Begin);
                         sw.Write(pageContent);
                         sw.Close();
                     }
                     catch (Exception ex) { }
     //HTTP向应后写出文件
     
    
    HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create("http://www.microsoft.com");
     WebResponse result = null;
     try
     {
         myWebRequest = (HttpWebRequest)WebRequest.Create(Url);
         result = myWebRequest.GetResponse();
         Stream response = result.GetResponseStream();
         byte[] bytes = new byte[10240];
         int n = 1;
     
        FileStream fs = File.Create(@"d:数据处理法规20080116HTML" + Actid.ToString() + ".html");
         while (n > 0)
         {
             n = response.Read(bytes, 0, 10240);
             fs.Write(bytes, 0, n);
         }
         response.Close();
         fs.Close();
     }
     catch (Exception ex) { }
     
    
    //读取
     
    string FileContent = File.ReadAllText(path, System.Text.Encoding.GetEncoding("gb2312"));
    

      

  • 相关阅读:
    代码
    (转载)计算机的二进制起源
    表的新建
    SQL约束
    包装类
    GUID(转载)
    Android九宫格解锁自定义控件(附源码)
    Android滑动页面返回(自定义控件)
    Android高仿QQ消息滑动删除(附源码)
    Android跟踪球-手势移动图片-自定义控件(附源码)
  • 原文地址:https://www.cnblogs.com/jameslif/p/3459746.html
Copyright © 2011-2022 走看看