Search code examples
c#text-filesread-write

How to remove specific number of line from existing file and create new text file


I have a text file which is have n number of line, I have extracted few line (suppose 10 lines) from the existing file. Now I want to delete those 10 line from the from the existing file and create the new file with all existing data after removing those 10 lines.

 private void ReWriteFile(string NewFileName)
 {            
    List<string> linetoDelete = _errorLine;

    using (StreamReader reader = new StreamReader(_fileName))
    {
       using (StreamWriter writer = new StreamWriter(NewFileName))
       {
               // reading the old file remove the error line and create new file                   

        }
     }
 }

Please help me with example.


Solution

  •     private void ReWriteFile(string NewFileName) {
    
            List<String> _errorLine = new List<string>() {
                "Error Line 1", "Error Line 2"
            };
    
            String _fileName = "InputFile.txt";
            String _outputFile = NewFileName;
    
            List<string> linetoDelete = _errorLine;
    
    
            String[] sourceLines = File.ReadAllLines(_fileName);
    
            if (sourceLines.Length > 0) {
                using (StreamWriter writer = new StreamWriter(_outputFile)) {
                    foreach(String line in sourceLines) {
                        if (!_errorLine.Contains(line)) {
                            writer.WriteLine(line);
                        }
                    }
                }
            }
        }
    

    Reading and writing to text files is a rudimentary part of programming, and your question lacks any evidence of attempting to solve the issue yourself.

    A few points to consider.

    1) When you are reading a text file for processing, load the lot into an array of String objects. The source will not change while you're processing it, so there's no need to keep a reader open holding a handle on the file.

    2) Deleting content from a file is a bad approach, as you are potentially changing the file while processing it. This will cause issues as you're enumerating the file at the time (assuming you are using a reader in a loop).

    3) My approach is not perfect - if the _errorLine list is long, and the source file is also long, there will be a lot of calls to check whether the _errorLines list contains a particular String. There will be a faster way to do this, but I haven't the time to do any extensive testing.

    4) As a follow on to point 3, this code is not tested and may need small adjustment to get it working correctly.

    Finally, when posting a question on SO, showing that you have at least tried to resolve the issue is a better option than just giving us a skeleton example and asking us to do your work for you.

    Please refer to the following pages for information on reading/writing from files.

    The number of fresh graduate programmers I've seen who can't code up basic IO functions like this is somewhat disconcerting!

    https://msdn.microsoft.com/en-GB/library/ezwyzy7b.aspx

    https://msdn.microsoft.com/en-GB/library/aa287548%28v=vs.71%29.aspx