【www.gdgbn.com--php函数】

asp教程.net c写文件函数实例代码
streamwriter 和 streamreader 向流写入字符并从流读取字符。下面的代码示例打开 log.txt 文件(如果文件不存在则创建文件)以进行输入,并将信息附加到文件尾。然后将文件的内容写入标准输出,以便显示出来。

[c#]
using system;
using system.io;
class dirappend
{
  public static void main(string[] args)
  {
  streamwriter w = file.appendtext("log.txt");
  log ("test1", w);
  log ("test2", w);
  // close the writer and underlying file.
  w.close();
  // open and read the file.
  streamreader r = file.opentext("log.txt");
  dumplog (r);
  }

  public static void log (string logmessage, textwriter w)
  {
  w.write("rnlog entry : ");
  w.writeline("{0} {1}", datetime.now.tolongtimestring(),
  datetime.now.tolongdatestring());
  w.writeline(" :");
  w.writeline(" :{0}", logmessage);
  w.writeline ("-------------------------------");
  // update the underlying file.
  w.flush();
  }

  public static void dumplog (streamreader r)
  {
  // while not at the end of the file, read and write lines.
  string line;
  while ((line=r.readline())!=null)
  {
  console.writeline(line);
  }
  r.close();
  }
}


读取文件二

using (filestream fs = new filestream(file, filemode.open,fileaccess.readwrite))
            {
                xmldocument toxml = new xmldocument();
                toxml.load(fs);
                //do some modification for the xml.
                fs.flush();
                toxml.save(fs);
            }


更多详细内容请查看:http://www.111cn.net/net/c/33608.htm


读取文件三

写文件

        public static void writefile(string filepath, string str)
        {
            streamwriter sr;
            if (file.exists(filepath)) //如果文件存在,则创建file.appendtext对象
            {
                sr = file.appendtext(filepath);
             }
            else   //如果文件不存在,则创建file.createtext对象
            {
                sr = file.createtext(filepath);
            } 
            sr.writeline(str);
            sr.close();
         }

 

本文来源:http://www.gdgbn.com/jiaocheng/26986/