文件读取并复制到另一个文件 [英] File Read and copy to another file

查看:79
本文介绍了文件读取并复制到另一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我是文件系统的新手.

我有一个文本文件名:sample中包含一些数据.

在该文件中:

您好http://www.codeproject.com/Message.aspxDomain:codeproject子域:www World

像这样的文本出现在文本文件中
我的问题:
我想读取该文件并将某些(http://www.codeproject.com/Message.aspx)行复制到另一个文件.


怎么做

请帮我解决问题

我知道如何读取文件.
但是如何逐行读取文件并将某些单词复制到另一个文件该怎么做呢???

请帮助
在此先感谢

Hi all

I am new to file system.

I have a text file name:sample having some data in it.

inside that file:

Hello http://www.codeproject.com/Message.aspxDomain:codeproject subDomain:www World

Like this text is present in the text file
My question:
i want to read that file and copy some(http://www.codeproject.com/Message.aspx ) line to another file.


How to do this

Please help me to solve the problem

I know how to read a file.
But how to read line by line of the file and copy to another file certain word how to do this????

please help
Thanks in advance

推荐答案

尝试:
string mySearchText = "http://";
using (StreamReader sr = new StreamReader(@"D:\Temp\sample.txt"))
    {
    using (StreamWriter sw = new StreamWriter(@"D:\Temp\MyOutputFile.txt"))
        {
        while (!sr.EndOfStream)
            {
            string s = sr.ReadLine();
            if (s.StartsWith(mySearchText))
                {
                sw.WriteLine(s);
                }
            }
        }
    }


//Create FileStream to hold txt file data
          System.IO.FileStream _fStream = File.OpenRead("Path");
          if (_fStream.CanRead)
          {
              //Read stream in StreamReader
              System.IO.StreamReader _strReader = new StreamReader(_fStream);
              //Read first line of text file you added
              string _txtFileFirstLine = _strReader.ReadLine();
              //if file contains atleast a line
              if (_txtFileFirstLine != string.Empty)
              {
                  //copy second file to copy this line inside it
                  System.IO.StreamWriter _strWriter =
                                            System.IO.File.AppendText("Path of   file to which you want to copy string");
                  //write line into second text file
                  _strWriter.WriteLine(_txtFileFirstLine);
                  //close write stream
                  _strWriter.Close();
                  _strWriter.Dispose();
              }
              //close read stream
              _strReader.Close();
              _strReader.Dispose();
          }
          //close file stream
          _fStream.Close();
          _fStream.Dispose();


这篇关于文件读取并复制到另一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆