反转 tsv/csv 文件或使用 asp.net 仅读取最后一行



我几乎被困在最近几天的一个问题上。我有一个文件,而位于远程服务器上,可以使用userId和密码进行访问。好吧,访问没有问题。

问题是我有大约 150 个。 它们中的每一个都是可变大小的最小为 2 MB,最大为 3 MB。

我必须一个接一个地读取它们并从中读取最后一行/最后一行数据。我正在我当前的代码中执行此操作。

主要问题是它花费了太多时间,因为它从上到下读取文件。

       public bool TEst(string ControlId, string FileName, long offset)
    {
        // The serverUri parameter should use the ftp:// scheme. 
        // It identifies the server file that is to be downloaded 
        // Example: ftp://contoso.com/someFile.txt. 
        // The fileName parameter identifies the local file. 
        //The serverUri parameter identifies the remote file. 
        // The offset parameter specifies where in the server file to start reading data. 
        Uri serverUri;
        String ftpserver = "ftp://xxx.xxx.xx.xxx/"+FileName;
        serverUri = new Uri(ftpserver);

        if (serverUri.Scheme != Uri.UriSchemeFtp)
        {
            return false;
        }
        // Get the object used to communicate with the server.
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
        request.Credentials = new NetworkCredential("test", "test");
        request.Method = WebRequestMethods.Ftp.DownloadFile;
       
        //request.Method = WebRequestMethods.Ftp.DownloadFile;
        
        request.ContentOffset = offset;
        FtpWebResponse response = null;
        try
        {
            response = (FtpWebResponse)request.GetResponse();
           // long Size = response.ContentLength;
           
        }
        catch (WebException e)
        {
            Console.WriteLine(e.Status);
            Console.WriteLine(e.Message);
            return false;
        }
       
        // Get the data stream from the response.
        Stream newFile = response.GetResponseStream();
        // Use a StreamReader to simplify reading the response data.
        StreamReader reader = new StreamReader(newFile);
        string newFileData = reader.ReadToEnd();
        // Append the response data to the local file 
        // using a StreamWriter.

        string[] parser = newFileData.Split('t');
        string strID = parser[parser.Length - 5];
        string strName = parser[parser.Length - 3];
        string strStatus = parser[parser.Length-1];
        if (strStatus.Trim().ToLower() != "suspect")
        {
            HtmlTableCell control = (HtmlTableCell)this.FindControl(ControlId);
            control.InnerHtml = strName.Split('.')[0];
        }
        else
        {
            HtmlTableCell control = (HtmlTableCell)this.FindControl(ControlId);
            control.InnerHtml = "S";
        }

        // Display the status description. 
        // Cleanup.
      
        reader.Close();
        response.Close();
        //Console.WriteLine("Download restart - status: {0}", response.StatusDescription);
        return true;
    }

线程:

  protected void Page_Load(object sender, EventArgs e)
  {
     
     new Task(()=>this.TEst("controlid1", "file1.tsv", 261454)).Start();
     new Task(()=>this.TEst1("controlid2", "file2.tsv", 261454)).Start();
  }

FTP无法查找仅读取最后几行的文件。 参考:FTP 命令 您必须与远程 ftp 服务器的开发人员和所有者协调,并要求他们制作一个包含所需数据的附加文件。

示例 要求远程 ftp 服务器的所有者为每个文件创建一个包含文件最后一行的 [文件名]_lastrow 文件。 然后,您的程序将对 [文件名]_lastrow 文件进行操作。 您可能会对"好的,我们可以为您做到这一点"的宽容回答感到惊喜。

如果无法更改 ftp 服务器,请请求数据库连接。

您还可以并行下载所有文件,并在完成后开始将它们弹出到队列中以进行解析,而不是同步执行此过程。如果 ftp 服务器可以处理更多连接,请使用尽可能多的连接。解析也可以并行完成。

更多阅读:System.Threading.Tasks

它有点被埋没了,但我在你的原始答案中发表了评论。这个SO问题导致了这篇博客文章,其中包含一些很棒的代码,您可以从中汲取。

您可以使用 Seek 直接跳到流的末尾,而不是您的 while 循环。然后,您希望在流中向后工作,直到找到第一个新行变量。 这篇文章应该给你你需要知道的一切。

获取最后 10 行非常大的文本文件> 10GB

FtpWebRequest 包含 ContentOffset 属性。查找/选择一种方法来保留最后一行的偏移量(本地或远程 - 即通过将 4 字节文件上传到 ftp)。这是最快的方法,也是网络流量的最佳方法。

有关 FtpWebRequest 的更多信息,请访问 MSDN

最新更新