asp.net c写文件函数实例代码

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

 

时间: 2024-09-20 00:15:56

asp.net c写文件函数实例代码的相关文章

asp.net StreamReader 创建文件的实例代码

这篇文章介绍了asp.net StreamReader 创建文件的实例代码,有需要的朋友可以参考一下   复制代码 代码如下: using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using

php中写文件函数实例程序

方法一,利用fopen与fwirte函数实现 1,PHP如何打开文件 使用PHP函数fopen()打开一个文件,fopen()一般使用2个参数表示打开文件的路径和文件模式.比如:  代码如下 复制代码 $fp=fopen("../cnbruce.txt",'w');   其中 "../cnbruce.txt" 就表示打开的cnbruce.txt文件的路径(相对当前执行程序文件的路径),'w'表示以只写的方式打开该文本文件. 写文件用 fwrite(file,stri

asp读取写文件fso实例代码

 '==============================  '函 数 名:FsoLineWrite  '作    用:按行写入文件  '参    数:文件相对路径FilePath,写入行号LineNum,写入内容LineContent  '==============================  Function FsoLineWrite(FilePath,LineNum,LineContent)   If LineNum<1 Then Exit Function   Set Fs

asp.net StreamReader 创建文件的实例代码_实用技巧

复制代码 代码如下: using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Drawing;using System.Web;using System.Web.SessionState;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.HtmlControls;us

PHP下载文件的函数实例代码_php实例

通过函数完成下载文件的PHP功能代码 function download($url, $filename) { // 获得文件大小, 防止超过2G的文件, 用sprintf来读 $filesize = sprintf ( "%u", filesize ( $url ) ); if (! $filesize) { return; } header ( "Content-type:application/octet-stream\n" ); //application/

PHP下载文件的函数实例代码

通过函数完成下载文件的PHP功能代码 function download($url, $filename) { // 获得文件大小, 防止超过2G的文件, 用sprintf来读 $filesize = sprintf ( "%u", filesize ( $url ) ); if (! $filesize) { return; } header ( "Content-type:application/octet-stream\n" ); //application/

asp OpenTextFile文本读取与写入实例代码_应用技巧

object.OpenTextFile(filename[, iomode[, create[, format]]])  参数  object :必选项.应为 FileSystemObject 对象的名称.  filename :必选项.字符串表达式,指明要打开的文件名称.  iomode :可选项.输入/输出模式,是下列三个常数之一:ForReading,ForWriting,或 ForAppending.  create :可选项.Boolean 值,指出当指定的 filename 不存在时

php实现当前页面点击下载文件的实例代码_php实例

php控制器中代码 public function downFile($path = ''){ if(!$path) header("Location: /"); download($path); } download文件下载函数代码 function download($file_url,$new_name=''){ if(!isset($file_url)||trim($file_url)==''){ echo '500'; } if(!file_exists($file_url)

php实现当前页面点击下载文件的实例代码

php控制器中代码 public function downFile($path = ''){ if(!$path) header("Location: /"); download($path); } download文件下载函数代码 function download($file_url,$new_name=''){ if(!isset($file_url)||trim($file_url)==''){ echo '500'; } if(!file_exists($file_url)