简体   繁体   English

写入文本文件

[英]writing to a text file

I have a text file with these contents 我有一个包含这些内容的文本文件

balamurugan,rajendran,chendurpandian
christopher
updateba

and i have read these files and searched for a keyword ba and i tried to write in another text file log.txt but after executing my code i am getting the third line only as 而且我已经阅读了这些文件并搜索了关键字ba ,我尝试在另一个文本文件log.txt进行写入,但是在执行我的代码后,我仅获得了第三行

`LineNo : 2 : updateba` 

I need to get both these lines 我需要这两行

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

I am using this code to write to a text file 我正在使用此代码写入文本文件

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>");
        }

i have used this sample link text 我已经使用了此示例链接文本

Any suggestion??? 有什么建议吗?

You are calling WriteAllText - this overwrites the file; 您正在调用WriteAllText这将覆盖文件; perhaps you should File.AppendAllText ? 也许你应该File.AppendAllText Or, more efficiently, use a StreamWriter in the first place - ie 或者,更有效地,首先使用StreamWriter

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

Reducing the code in the question to something like the minimal key code, something like: 在问题的代码减少类似的最小关键代码,是这样的:

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 File.WriteAllText

Creates a new file, write the contents to the file, and then closes the file. 创建一个新文件,将内容写入该文件,然后关闭该文件。 If the target file already exists, it is overwritten. 如果目标文件已经存在,则将其覆盖。

Source. 资源。 http://msdn.microsoft.com/en-us/library/system.io.file.writealltext.aspx http://msdn.microsoft.com/zh-CN/library/system.io.file.writealltext.aspx

You probably want to make a buffer and write the buffer the file after you are done. 完成之后,您可能想要创建一个缓冲区并将缓冲区写入文件。

edit damn it 20 seconds too late. 编辑该死的20秒为时已晚。

您需要AppendAllText

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM