My StreamWriter does not pick up the last line and curious as to why.

So I am making a streamreader/streamwriter which takes data from one file, manipulates the data line by line and spits it out into another file.  Seems simple enough, right?  Now the issue that I am having is that my StreamWriter does not write down the final record or final line in my project, the last line that it spits out is the second to last line.  Why it does this?  I have no clue.  Perhaps I can show the code and you guys can take a quick look and tell me why this may or may not happen.  Thanks!

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;


namespace FileReaderandWriter20App
{
    class Program
    {        
        string filelocation2 = @"C:\Users\H34829\Desktop\details\after\DetailsBefore.txt";
        static void Main(string[] args)
        {     
            string filelocation = @"C:\Users\H34829\Desktop\details\before\DetailsAfter.txt";

            Program program = new Program();

            program.ReadFile(filelocation);

            
        }



        public void ReadFile(string filename)
        {
            //string line = null;
            string searchTarget = "~";
            string doublesearchTarget = "~~";
            string tripesearchTarget = "~~~";

            File.Delete(filelocation2);

            try
            {
                using (FileStream fs = new FileStream(filename, FileMode.Open))
                {
                    using (StreamReader reader = new StreamReader(fs, Encoding.UTF8))
                    {
                        string line = null;

                        while ((line = reader.ReadLine()) != null)
                        {

                            if (line.Contains("~"))
                            {

                                if (!File.Exists(filelocation2))
                                {
                                    try
                                    {
                                        using (FileStream fs1 = new FileStream(filelocation2, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                                        {
                                            line = reader.ReadLine();
                                            line = Regex.Replace(line, "~", "~" + System.Environment.NewLine);

                                            using (StreamWriter writer = new StreamWriter(fs1))
                                            {
                                                Console.WriteLine(line);
                                                writer.WriteLine(line);
                                                writer.Flush();
                                            }
                                        }

                                    }
                                    catch (Exception ex)
                                    {
                                        Console.WriteLine(ex);
                                    }

                                }
                                else
                                {
                                    try
                                    {
                                        using (FileStream fs1 = new FileStream(filelocation2, FileMode.Append, FileAccess.Write))
                                        {
                                            line = reader.ReadLine();
                                            line = Regex.Replace(line, "~", "~" + System.Environment.NewLine);

                                            using (StreamWriter writer = new StreamWriter(fs1))
                                            {
                                                Console.WriteLine(line);
                                                writer.WriteLine(line);
                                                writer.Flush();
                                            }
                                        }

                                    }
                                    catch (Exception ex)
                                    {
                                        Console.WriteLine(ex);
                                    }
                                }
                            }
                            else
                            {
                                if (!File.Exists(filelocation2))
                                {
                                    try
                                    {
                                        using (FileStream fs1 = new FileStream(filelocation2, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                                        {
                                            line = reader.ReadLine();
                                            
                                            using (StreamWriter writer = new StreamWriter(fs1))
                                            {
                                                Console.WriteLine(line);
                                                writer.WriteLine(line);
                                                writer.Flush();
                                            }
                                        }

                                    }
                                    catch (Exception ex)
                                    {
                                        Console.WriteLine(ex);
                                    }

                                }
                                else
                                {
                                    try
                                    {
                                        using (FileStream fs1 = new FileStream(filelocation2, FileMode.Append, FileAccess.Write))
                                        {
                                            line = reader.ReadLine();                                            

                                            using (StreamWriter writer = new StreamWriter(fs1))
                                            {
                                                Console.WriteLine(line);
                                                writer.WriteLine(line);
                                                writer.Flush();
                                            }
                                        }

                                    }
                                    catch (Exception ex)
                                    {
                                        Console.WriteLine(ex);
                                    }
                                }
                            }
                        }

                    }
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

Open in new window

VBBRettAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
I wonder why you have this line:
line = reader.ReadLine();

inside the while ( ( line = reader.ReadLine() != null)  loop?
this will skip some lines in the file?!
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
I also wonder why you "complicated" all that code...
are you sure you need to have all that if/else like that?

why not check the filelocation2 existance once (before reading the file), creating it if needed, and just appending to it ...

in short:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;


namespace FileReaderandWriter20App
{
    class Program
    {        
        string filelocation2 = @"C:\Users\H34829\Desktop\details\after\DetailsBefore.txt";
        static void Main(string[] args)
        {     
            string filelocation = @"C:\Users\H34829\Desktop\details\before\DetailsAfter.txt";

            Program program = new Program();

            program.ReadFile(filelocation);

            
        }



        public void ReadFile(string filename)
        {
            //string line = null;
            string searchTarget = "~";
            string doublesearchTarget = "~~";
            string tripesearchTarget = "~~~";


            try
            {
            File.Delete(filelocation2);

            using (FileStream fs1 = new FileStream(filelocation2, FileMode.OpenOrCreate, FileAccess.ReadWrite)
            {
               using (StreamWriter writer = new StreamWriter(fs1))
               {


                using (FileStream fs = new FileStream(filename, FileMode.Open))
                {
                    using (StreamReader reader = new StreamReader(fs, Encoding.UTF8))
                    {
                        string line = null;

                        while ((line = reader.ReadLine()) != null)
                        {
                             line = Regex.Replace(line, "~", "~" + System.Environment.NewLine);
			     Console.WriteLine(line);
                             writer.WriteLine(line);
                             writer.Flush();
                        } // while reader.ReadLine

                    } //using new StreamReader
                } // using FileStream
                  } // using StreamWriter
               } // using FileStram
             }
             catch (Exception ex)
             {
             	Console.WriteLine(ex);
             }


        } // void ReadFile
                                  

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
VBBRettAuthor Commented:
Thank you very much.  Now one more thing, how would I remove the following character if they are in 1 line by themselves ~
basically, if there is just one ~ character in a line, I want it deleted.  How could I do that in a stream?  Thanks!
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
before this line:
line = Regex.Replace(line, "~", "~" + System.Environment.NewLine);

Open in new window

I would add this bit of code
 if line.Length > 0 Then
Dim Occurrences As Integer = (line.Length - line.Replace("~", String.Empty).Length) / line.Length
if Occurrences = 1 Then
   line = line.Replace("~", String.Empty).
End if 
End If

Open in new window

this should do the job
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.