asp.net C#文件读取文本实例函数

asp教程.net c#文件读取文本实例函数

下面的代码示例读取整个文件,并在检测到文件尾时发出通知。
[c#]
using system;
using system.io;
public class textfromfile {
  private const string file_name = "myfile.txt";
  public static void main(string[] args) {
  if (!file.exists(file_name)) {
  console.writeline("{0} does not exist!", file_name);
  return;
  }
  streamreader sr = file.opentext(file_name);
  string input;
  while ((input=sr.readline())!=null) {
  console.writeline(input);
  }
  console.writeline ("the end of the stream has been reached.");
  sr.close();
  }
}

此代码通过调用 file.opentext 创建一个指向 myfile.txt 的 streamreader。streamreader.readline 将每一行都返回为一个字符串。当没有要读取的字符时,一条消息显示该情况,然后流关闭。

向文件写入文本

下面的代码示例创建一个新文本文件并向其写入一个字符串。
[c#]
using system;
using system.io;
public class texttofile {
  private const string file_name = "myfile.txt";
  public static void main(string[] args) {
  if (file.exists(file_name)) {
  console.writeline("{0} already exists!", file_name);
  return;
  }
  streamwriter sr = file.createtext(file_name);
  sr.writeline ("this is my file.");
  sr.writeline ("i can write ints {0} or floats {1}, and so on.",
  1, 4.2);
  sr.close();
  }
}

如何把streamreader关联到filestream上。其优点是可以显式指定是否创建文件和共享许可,如果直接把streamreader关联到文件上,就不能这么做:
filestream fs = new filestream(@"c:"my documents"readme.txt", 
filemode.open, fileaccess.read, fileshare.none);
streamreader sr = new streamreader(fs); 
对于本例,指定streamreader查找字节码标记,以确定使用了什么编码方法,以后的示例也是这样,从一个fileinfo实例中获得streamreader:
fileinfo myfile = new fileinfo(@"c:"my documents"readme.txt");
streamreader sr = myfile.opentext();
与filestream一样,应在使用后关闭streamreader。如果没有这样做,就会致使文件一直锁定,因此不能执行其他过程(除非使用filestream构造streamreader和特定的fileshare. sharereadwrite):
sr.close();
介绍完实例化streamreader后,就可以用该实例作一些工作了。与filestream一样,我们仅指出可以用于读取数据的许多方式,您应在sdk文档说明书中查阅其他不太常用的streamreader方法。
所使用的最简单的方式是readline(),该方法一次读取一行,但返回的字符串中不包括标记该行结束的回车换行符:
string nextline = sr.readline();
另外,还可以在一个字符串中提取文件的所有剩余内容(严格地说,是流的全部剩余内容):
string restofstream = sr.readtoend();
可以只读取一个字符:
int nextchar = sr.read();
read()的重载方法可以把返回的字符转换为一个整数,如果到达流的尾端,就返回-1。
最后,可以用一个偏移值,把给定个数的字符读到数组中:
// to read 100 characters in.
int nchars = 100;
char [] chararray = new char[nchars]; 
int ncharsread = sr.read(chararray, 0, nchars);
如果要求读取的字符数多于文件中剩余的字符数,ncharsread应小于nchars 。
2. streamwriter类
streamwriter类的工作方式与streamreader的类似,但streamwriter只能用于写入文件(或另一个流)。构造streamwriter的方法包括:
streamwriter sw = new streamwriter(@"c:"my documents"readme.txt");
上面的代码使用了utf8编码方法,.net把这种编码方法设置为默认的编码方法。如果要指定其他的编码方法:
streamwriter sw = new streamwriter(@"c:"my documents"readme.txt", true,
  encoding.ascii);
在这个构造函数中,第二个参数是boolean型,表示文件是否应以追加方式打开。构造函数的参数不能仅是一个文件名和一个编码类。
当然,可以把streamwriter关联到一个文件流上,以获得打开文件的更多控制选项:
filestream fs = new filestream(@"c:"my documents"readme.txt", 
  filemode.createnew, fileaccess.write, fileshare.read);
streamwriter sw = new streamwriter(fs); 
fileinfo不执行返回streamwriter的任何方法。 
另外,如果要创建一个新文件,并开始给它写入数据,可以使用下面的代码:
fileinfo myfile = new fileinfo(@"c:"my documents"newfile.txt");
streamwriter sw = myfile.createtext();
与其他流类一样,在使用完后,要关闭streamwriter:
sw.close();
写入流可以使用streamwriter.write()的4个重载方法来完成。最简单的方式是写入一个流,后面加上一个回车换行符:
string nextline = "groovy line";
sw.write(nextline);
也可以写入一个字符:
char nextchar = ~a~;
sw.write(nextchar);
也可以写入一个字符数组:
char [] chararray = new char[100];
// initialize these characters
sw.write(chararray);
甚至可以写入字符数组的一部分:
int ncharstowrite = 50;
int startatlocation = 25;
char [] chararray = new char[100];
// initialize these characters
sw.write(chararray, startatlocation, ncharstowrite);
3.readwritetext示例
readwritetext示例说明了streamreader和streamwriter类的用法。它非常类似于前面的readbinaryfile示例,但假定要读取的文件是一个文本文件,并显示其内容。它还可以保存文件(包括在文本框中对文本进行的修改)。它将以unicode格式保存文件。
图30-9所示的readwritetext用于显示前面的newfile.aspx文件。但这次读取内容会更容易一些。
这里不打算介绍给打开文件对话框添加事件处理程序的详细内容,因为它们基本上与前面的binaryfilereader示例相同。与这个示例相同,打开一个新文件,将调用displayfile()方法。其惟一的区别是displayfile的执行方式,本例有一个保存文件的选项。这由另一个菜单项save来表示,这个选项的处理程序调用我们添加到代码中的另一个方法savefile()(

时间: 2024-08-31 14:39:09

asp.net C#文件读取文本实例函数的相关文章

php文件读取方法实例分析

  本文实例讲述了php文件读取方法.分享给大家供大家参考.具体如下: ? 1 2 3 4 5 <?php $file = fopen("Test//file.txt", "r"); //打开文件 echo fgetc($file); //读取文件中的一个字符 fclose($file); //关闭文件 ?> ? 1 2 3 4 5 <?php $file = fopen("Test//file.txt", "r&qu

php文件读取方法实例分析_php技巧

本文实例讲述了php文件读取方法.分享给大家供大家参考.具体如下: <?php $file = fopen("Test//file.txt", "r"); //打开文件 echo fgetc($file); //读取文件中的一个字符 fclose($file); //关闭文件 ?> <?php $file = fopen("Test//file.txt", "r"); //打开文件 echo fgets($f

asp.net多文件上传实例讲解_实用技巧

文件上传简单实现是非常容易的,但是想要更高的要求,比如通过ajax上传文件.一次上传多个文件.文件比较大等等,这里面的坑就不是很容易填(对于新手来说).因此在这里我准备通过ajax实现多文件上传.在开始贴代码之前,要注意几点: 1.<input type="file" />是必须要加name的,不知道为什么不加name属性,后台获取不到文件数据(有办法的大神可以在评论区提醒我),然后是multiple属性,当multiple="multiple"时,fi

asp无组件文件上传实例

<!--#include file="pf_upload_class.asp"--> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.111cn.net/1999/x

ASP操作XML文件的完整实例

<% '---------------------------------------------------------------- '程序简介: 完成asp语言对XML文档中指定节点文本的增加.删除.修改.查看 '入口参数: 无 '出口参数: 无 '------------------------------------------------ '函数名字:ConnectXml() '入口参数: filename 需要连接或打开的xml文件名 '出口参数: 无 '返回值 :ConnectX

ASP操作XML文件的完整实例代码

复制代码 代码如下: <% '---------------------------------------------------------------- '程序简介: 完成asp语言对XML文档中指定节点文本的增加.删除.修改.查看 '入口参数: 无 '出口参数: 无 '------------------------------------------------ '函数名字:ConnectXml() '入口参数: filename 需要连接或打开的xml文件名 '出口参数: 无 '返回

python中readline判断文件读取结束实例

例  代码如下 复制代码 filename = raw_input('Enter your file name')  #输入要遍历读取的文件路径及文件名 file = open(filename,'r') done = 0 while not  done:         aLine = file.readline()         if(aLine != ''):             print aLine,         else:             done = 1 file

c++-C++随机读取文件,不用getline()函数怎么做

问题描述 C++随机读取文件,不用getline()函数怎么做 在文件保存数据,文件名为inputfile.txt,文件中的数据格式为: (a,n)-(a,n)(0,0) (a,n)-(a,n)(0,0) AND(#,#) 其中,a是double型数据,n是int型数据,两数之间使用逗号分隔,并用括号括起来.随机读取文件,要求以(0,0)分别作为第一次和第二次读取的结束.以(#,#)作为全部数据读取的结束标志.不用getline()函数,怎么做: 解决方案 文件读取之getline函数C++依次

.net精简框架集下的ini文件读取(C#)

        无论是.net框架集还是.net精简框架集都能非常完美的支持XML文件,并且微软也强烈建议用xml文件取代ini文件,但是在工控领域大部分的工程都是采用ini文件配置的系统信息的.        以前的隧道管理系统是采用ini文件配置信息的,为了实现兼容,只好在下位机也采用ini文件配置信息.由于WinCE平台不提供ini文件读取的api函数GetPrivateProfileString,所以需要自行编写,前一段时间提供了一个EVC版本的ini文件读取,后来在DA66x的WinC