16

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.Take(lines.Length - 1).ToArray());

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

  • Maybe not the answer you are looking for but how about just replacing the bytes representing the first line to NULL? Next time you can File.ReadAllLines, it will skip over all the initial NULL bytes – Polity Aug 10 '11 at 09:50

5 Answers5

18

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().

Sedat Kapanoglu
  • 46,641
  • 25
  • 114
  • 148
  • 1
    It is inefficient yes - but is there any way to truncate the beginning of a file? – Anders Abel Aug 10 '11 at 09:29
  • Yes you just omit writing it as stated in my answer. – Sedat Kapanoglu Aug 10 '11 at 09:29
  • 2
    @ssg: If you omit the writing part then how does the file get updated? I assume I'm missing something... – Chris Aug 10 '11 at 09:43
  • 1
    @Chris You can't "truncate" the start of a file, you have to read the entire file and then write back only the parts of the file that you want (in this case everything except for the first line). – Justin Aug 10 '11 at 10:04
5

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.
        }
    }
jgauffin
  • 99,844
  • 45
  • 235
  • 372
4
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.

Anders Abel
  • 67,989
  • 17
  • 150
  • 217
0

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();
Martin
  • 3,396
  • 5
  • 41
  • 67
0

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.

Shubham Khare
  • 83
  • 1
  • 11