我想从文本文件中输出一段文字,请问如何解决?谢谢!

解决方案 »

  1.   

    Use a TextReader to read the source file, and use a TextWriter to write to the destination file.
      

  2.   

    //读/写文本文件示例代码:
    读:
    FileInfo fi = new FileInfo (_filepath);
    FileStream fs;
    //将文件读取到文件流中
    fs = fi.OpenRead ();
    //声明一读取文件流类的实例,用以读取文件流
    StreamReader sr = new StreamReader (fs);
    string myreader = sr.ReadLine() ;//文件数据
    while(myreader != null)
    {
        ...//做处理
    myreader = sr.ReadLine();
    }
    写:
    private void WriteLog(string msg)
    {
    StreamWriter sw = null;
    try
    {
    if(_logName == null || _logName == "")
    {
    _logName = this._filepath.Substring(0,_filepath.LastIndexOf(@"\")+1)+"PortLog.txt" ;
    }
    if(Directory.Exists(_logName.Substring(0,_logName.LastIndexOf(@"\"))))
    {
    if(File.Exists(_logName))
    sw = File.AppendText(_logName ) ;
    else
    sw = File.CreateText(_logName ) ;
    sw.WriteLine(System.DateTime.Now.ToString()+"-"+msg) ;
    sw.Flush() ;
    }
    }
    catch(Exception)
    {
    throw new Exception("Error occured when writing log.") ;
    }
    finally
    {
    sw.Close() ;
    }
    }
      

  3.   

    那不就是先读出来再写到另一个文件里吗,Easy
    楼上正解