簡體   English   中英

寫入文本文件

[英]writing to a text file

我有一個包含這些內容的文本文件

balamurugan,rajendran,chendurpandian
christopher
updateba

而且我已經閱讀了這些文件並搜索了關鍵字ba ,我嘗試在另一個文本文件log.txt進行寫入,但是在執行我的代碼后,我僅獲得了第三行

`LineNo : 2 : updateba` 

我需要這兩行

LineNo : 0 : balamurugan,rajendran,chendurpandian
LineNo : 2 : updateba

我正在使用此代碼寫入文本文件

if (File.Exists(FilePath))
        {
            // Read the file and display it line by line.
            System.IO.StreamReader file = new System.IO.StreamReader(FilePath);
            while ((line = file.ReadLine()) != null)
            {
                if (line.Contains(regMatch))
                {
                    DirectoryInfo Folder = new DirectoryInfo(textboxPath.Text);
                    if (Folder.Exists)
                    {
                        var dir = @"D:\New folder\log";
                        if (!Directory.Exists(dir))
                        {
                            Directory.CreateDirectory(dir);
                        }
                        File.WriteAllText(Path.Combine(dir, "log.txt"), "LineNo : " + counter.ToString() + " : " + line + "<br />");

                    }
                    else
                    {
                        Response.Write("<script language='javascript'>window.alert('Folder not found');</script>");
                    }
                    Response.Write("<script language='javascript'>window.alert('Pattern found');</script>");
                    Response.Write("LineNo : " + counter.ToString()+ " : " + line + "<br />");
                }

                else
                {
                    Response.Write("<script language='javascript'>window.alert('Pattern not found');</script>");
                }
                counter++;


            }
               file.Close();
        }
        else
        {
            Response.Write("<script language='javascript'>window.alert('File not found');</script>");
        }

我已經使用了此示例鏈接文本

有什么建議嗎?

您正在調用WriteAllText這將覆蓋文件; 也許你應該File.AppendAllText 或者,更有效地,首先使用StreamWriter

using (var dest = File.CreateText(path))
{
    while (loopCondition)
    {
        // snip
        dest.WriteLine(nextLineToWrite);
    }
}

在問題的代碼減少類似的最小關鍵代碼,是這樣的:

DirectoryInfo Folder = new DirectoryInfo(textboxPath.Text);
var dir = @"D:\New folder\log";
if (Folder.Exists)
{                
    if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
}

if (File.Exists(FilePath))
{
    // Read the file and display it line by line.
    using (var file = File.OpenText(FilePath))
    using (var dest = File.AppendText(Path.Combine(dir, "log.txt")))
    {
        while ((line = file.ReadLine()) != null)
        {
            if (line.Contains(regMatch))
            {
                dest.WriteLine("LineNo : " + counter.ToString() + " : " +
                     line + "<br />");
            }
            counter++;
        }
    }
}

File.WriteAllText

創建一個新文件,將內容寫入該文件,然后關閉該文件。 如果目標文件已經存在,則將其覆蓋。

資源。 http://msdn.microsoft.com/zh-CN/library/system.io.file.writealltext.aspx

完成之后,您可能想要創建一個緩沖區並將緩沖區寫入文件。

編輯該死的20秒為時已晚。

您需要AppendAllText

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM