从文本文件中删除特定行? [英] Delete specific line from a text file?

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

问题描述

我需要删除一个文本文件的确切行,但我不能为我的锻炼如何去这样做的使用寿命。

I need to delete an exact line from a text file but I cannot for the life of me workout how to go about doing this.

任何建议或例子会大大AP preciated?

Any suggestions or examples would be greatly appreciated?

相关问题

<一个href=\"http://stackoverflow.com/questions/532217/efficient-way-to-delete-a-line-from-a-text-file-c\">Efficient方法来删除一个文本文件中的行(C#)

推荐答案

如果您要删除该行是基于行的内容:

If the line you want to delete is based on the content of the line:

string line = null;
string line_to_delete = "the line i want to delete";

using (StreamReader reader = new StreamReader("C:\\input")) {
    using (StreamWriter writer = new StreamWriter("C:\\output")) {
        while ((line = reader.ReadLine()) != null) {
            if (String.Compare(line, line_to_delete) == 0)
                continue;

            writer.WriteLine(line);
        }
    }
}

或者,如果它是基于行数:

Or if it is based on line number:

string line = null;
int line_number = 0;
int line_to_delete = 12;

using (StreamReader reader = new StreamReader("C:\\input")) {
    using (StreamWriter writer = new StreamWriter("C:\\output")) {
        while ((line = reader.ReadLine()) != null) {
            line_number++;

            if (line_number == line_to_delete)
                continue;

            writer.WriteLine(line);
        }
    }
}

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

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