有效的方法来删除一个文本文件中的行 [英] Efficient way to delete a line from a text file

查看:135
本文介绍了有效的方法来删除一个文本文件中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从一个文本文件中删除一定行。什么是这样做的最有效的方法是什么?文件可以是潜在的大(超过万条记录)。

更新:
下面是我目前使用的code,但我不知道这是否是好的。

 内部空隙DeleteMarkedEntries(){
    字符串TEMPPATH = Path.GetTempFileName();
    使用(VAR读者=新的StreamReader(LOGPATH)){
        使用(VAR作家=新的StreamWriter(File.OpenWrite(TEMPPATH))){
            INT计数器= 0;
            而(!reader.EndOfStream){
                如果(!_deletedLines.Contains(计数器)){
                    writer.WriteLine(reader.ReadLine());
                }
                ++计数器;
            }
        }
    }
    如果(File.Exists(TEMPPATH)){
        File.Delete(日志路径);
        File.Move(TEMPPATH,日志路径);
    }
}


解决方案

这样做的最直接的方式可能是最好的,写整个文件到一个新的文件,写的所有行,除了一个()你不想要的。

另外,打开文件进行随机访问的文件。

阅读到你想删除该行的地步。
跳过删除线,并且读取的字节的数量(包括CR + LF - 如果需要的话),写这个数字在删除行的字节,推进由字节的数量这两个地点和重复,直到文件结尾

希望这有助于。

修改 - 现在,我可以看到你的code

 如果(!_deletedLines.Contains(计数器))
{
    writer.WriteLine(reader.ReadLine());
}

就不行,如果行,你不想要的,您仍想读只是把它写的。以上code既不会读或写出来。新的文件将是完全一样的老

您想要的东西像

 串线= reader.ReadLine();
如果(!_deletedLines.Contains(计数器))
{
    writer.WriteLine(线);
}

I need to delete a certain line from a text file. What is the most efficient way of doing this? File can be potentially large(over million records).

UPDATE: below is the code I'm currently using, but I'm not sure if it is good.

internal void DeleteMarkedEntries() {
    string tempPath=Path.GetTempFileName();
    using (var reader = new StreamReader(logPath)) {
        using (var writer = new StreamWriter(File.OpenWrite(tempPath))) {
            int counter = 0;
            while (!reader.EndOfStream) {
                if (!_deletedLines.Contains(counter)) {
                    writer.WriteLine(reader.ReadLine());
                }
                ++counter;
            }
        }
    }
    if (File.Exists(tempPath)) {
        File.Delete(logPath);
        File.Move(tempPath, logPath);
    }
}

解决方案

The most straight forward way of doing this is probably the best, write the entire file out to a new file, writing all lines except the one(s) you don't want.

Alternatively, open the file for random access.

Read to the point where you want to "delete" the line. Skip past the line to delete, and read that number of bytes (including CR + LF - if necessary), write that number of bytes over the deleted line, advance both locations by that count of bytes and repeat until end of file.

Hope this helps.

EDIT - Now that I can see your code

if (!_deletedLines.Contains(counter)) 
{                            
    writer.WriteLine(reader.ReadLine());                        
}

Will not work, if its the line you don't want, you still want to read it, just not write it. The above code will neither read it or write it. The new file will be exactly the same as the old.

You want something like

string line = reader.ReadLine();
if (!_deletedLines.Contains(counter)) 
{                            
    writer.WriteLine(line);                        
}

这篇关于有效的方法来删除一个文本文件中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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