CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  ASP.NET

如何逐行读取文本内容?

楼主jqryga1221()2005-09-23 15:48:42 在 .NET技术 / ASP.NET 提问

用StreamReader或是File类? 问题点数:50、回复次数:3Top

1 楼zykj_2000(向MVP努力)回复于 2005-09-23 15:50:23 得分 20

MSDN中的例子  
  示例  
  [Visual   Basic,   C#,   C++]   下面的示例从文件中读取行,一直读到文件尾。  
  [Visual   Basic]    
  Imports   System  
  Imports   System.IO  
  Imports   System.Text  
   
  Public   Class   Test  
   
          Public   Shared   Sub   Main()  
                  Dim   path   As   String   =   "c:\temp\MyTest.txt"  
   
                  Try  
                          If   File.Exists(path)   Then  
                                  File.Delete(path)  
                          End   If  
   
                          Dim   sw   As   StreamWriter   =   New   StreamWriter(path)  
                          sw.WriteLine("This")  
                          sw.WriteLine("is   some   text")  
                          sw.WriteLine("to   test")  
                          sw.WriteLine("Reading")  
                          sw.Close()  
   
                          Dim   sr   As   StreamReader   =   New   StreamReader(path)  
   
                          Do   While   sr.Peek()   >=   0  
                                  Console.WriteLine(sr.ReadLine())  
                          Loop  
                          sr.Close()  
                  Catch   e   As   Exception  
                          Console.WriteLine("The   process   failed:   {0}",   e.ToString())  
                  End   Try  
          End   Sub  
  End   Class  
  [C#]    
  using   System;  
  using   System.IO;  
   
  class   Test    
  {  
                  public   static   void   Main()    
          {  
                  string   path   =   @"c:\temp\MyTest.txt";  
                  try    
                  {  
                          if   (File.Exists(path))    
                          {  
                                  File.Delete(path);  
                          }  
   
                          using   (StreamWriter   sw   =   new   StreamWriter(path))    
                          {  
                                  sw.WriteLine("This");  
                                  sw.WriteLine("is   some   text");  
                                  sw.WriteLine("to   test");  
                                  sw.WriteLine("Reading");  
                          }  
   
                          using   (StreamReader   sr   =   new   StreamReader(path))    
                          {  
                                  while   (sr.Peek()   >=   0)    
                                  {  
                                          Console.WriteLine(sr.ReadLine());  
                                  }  
                          }  
                  }    
                  catch   (Exception   e)    
                  {  
                          Console.WriteLine("The   process   failed:   {0}",   e.ToString());  
                  }  
          }  
  }  
  Top

2 楼yuezhong(妈咪,有小姐吗?便宜点的)回复于 2005-09-23 15:53:03 得分 20

StreamReader   sr=new   StreamReader(   路径   );  
  string   strLine   =   sr.ReadLine();  
  while(strLine!=   null)  
  {  
        strLine   =   sr.ReadLine();  
  }Top

3 楼YYKXQ(异域狂想曲)回复于 2005-09-23 15:53:21 得分 10

StreamReader   sr=new   StreamReader("文本文件路径\\文件名",System.Text.Encoding.Default);  
  //调用ReadLine方法一行一行的读  
  string   str=sr.ReadLine();//第一行  
  string   str1=sr.ReadLine();//第二行  
  ...Top

相关问题

  • 逐行读取文本框中内容?
  • pb6.5如何逐行读取一个文本文件?
  • 读取文本内容的问题
  • asp读取文本内容,求助啊~~~~
  • delphi中如何从unix格式的文本文件中逐行读取
  • 怎样逐行的读取文本文件,象vb的readline那样。
  • 如何读取文本文件中的每行以“,”分割的数据,并逐行读取???
  • 如何在oracle中保存文本,读取文本内容?
  • 如何逐行读文本文件的内容?
  • 请问怎样实现将memo框中的文本逐字逐行的读取出来?

关键词

  • c#
  • streamreader
  • sw
  • sr
  • writeline
  • readline
  • streamwriter
  • strline
  • path
  • console

得分解答快速导航

  • 帖主:jqryga1221
  • zykj_2000
  • yuezhong
  • YYKXQ

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo