开发者

Removing the first line of a text file in C#

I can currently remove the last line of a text file using:

    var lines = System.IO.File.ReadAllLines("test.txt");
    System.IO.File.WriteAllLines("test.txt", lines.Tak开发者_开发百科e(lines.Length - 1).ToArray());

Although, how is it possible to instead remove the beginning of the text file?


Instead of lines.Take, you can use lines.Skip, like:

var lines = File.ReadAllLines("test.txt");
File.WriteAllLines("test.txt", lines.Skip(1).ToArray());

to truncate at the beginning despite the fact that the technique used (read all text and write everything back) is very inefficient.

About the efficient way: The inefficiency comes from the necessity to read the whole file into memory. The other way around could easily be to seek in a stream and copy the stream to another output file, delete the original, and rename the old. That one would be equally fast and yet consume much less memory.

Truncating a file at the end is much easier. You can just find the trunaction position and call FileStream.SetLength().


Here is an alternative:

        using (var stream = File.OpenRead("C:\\yourfile"))
        {
            var items = new LinkedList<string>();
            using (var reader = new StreamReader(stream))
            {
                reader.ReadLine(); // skip one line
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    //it's far better to do the actual processing here
                    items.AddLast(line);
                }
            }
        }

Update

If you need an IEnumerable<string> and don't want to waste memory you could do something like this:

    public static IEnumerable<string> GetFileLines(string filename)
    {
        using (var stream = File.OpenRead(filename))
        {
            using (var reader = new StreamReader(stream))
            {
                reader.ReadLine(); // skip one line
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    yield return line;
                }
            }
        }
    }


    static void Main(string[] args)
    {
        foreach (var line in GetFileLines("C:\\yourfile.txt"))
        {
            // do something with the line here.
        }
    }


var lines = System.IO.File.ReadAllLines("test.txt");
System.IO.File.WriteAllLines("test.txt", lines.Skip(1).ToArray());

Skip eliminates the given number of elements from the beginning of the sequence. Take eliminates all but the given number of elements from the end of the sequence.


To remove fist line from a text file

System.IO.StreamReader file = new System.IO.StreamReader(filePath);
string data = file.ReadToEnd();
file.Close();
data = Regex.Replace(data, "<.*\n", "");
System.IO.StreamWriter file = new System.IO.StreamWriter(filePath, false);
file.Write(data);
file.Close();


can do in one line also

File.WriteAllLines(origialFilePath,File.ReadAllLines(originalFilePath).Skip(1));

Assuming you are passing your filePath as parameter to the function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜