StreamReader实例

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.IO;

/// <summary>
/// file 的摘要说明
/// </summary>

public class file
{
    
public file()
    
{
        
//
        
// TODO: 在此处添加构造函数逻辑
        
//
    }

    
static void Main()
    
{
        StreamReader objreader 
= new StreamReader("F:\\upfile\\t.txt");
        
string sline="";
        ArrayList arrtext 
= new ArrayList();
        
while (sline != null)
        
{
            sline 
= objreader.ReadLine();
            
if (sline != null)
                arrtext.Add(sline);

        }
 objreader.Close();
        
foreach(string ss in arrtext)
            Console.WriteLine(ss);
            Console.ReadLine();

    }

}

写文件
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)) 
            
{
                
//This allows you to do one Read operation.
                Console.WriteLine(sr.ReadToEnd());
            }

        }
 
        
catch (Exception e) 
        
{
            Console.WriteLine(
"The process failed: {0}", e.ToString());
        }

    }

}

原文地址:https://www.cnblogs.com/binlyzhuo/p/1218803.html