Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8561273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:27:59+00:00 2026-06-11T16:27:59+00:00

I am trying to write text file after reading from a large tab delimited

  • 0

I am trying to write text file after reading from a large tab delimited text file in a console application. Issue is that if I run many instances of this exe concurrently on a server, it gives me runtime error at TextWriter.WriteLine and then application crashes because of unhandled exception. This happens with all the instances. I fail to understand the reason for this behavior.. Is it because I am not using StringBuilder which will use memory dynamically ?

My code is as follows :

 StreamReader sr = new StreamReader(@FilePath, System.Text.Encoding.Default);
 string mainLine = sr.ReadLine();
 string[] fileHeaders = mainLine.Split(new string[] { "\t" }, StringSplitOptions.None);
 string newLine = "";

 System.IO.StreamWriter outFileSw = new System.IO.StreamWriter(@outFile);

 while (!sr.EndOfStream)
 {
    mainLine = sr.ReadLine();
    string[] originalLine = mainLine.Split(new string[] { "\t" }, StringSplitOptions.None);

   newLine = "";
   for (int i = 0; i < fileHeaders.Length; i++)
   {
      if(fileHeaders[i].Trim() != "")
       newLine = newLine + fileHeaders[i].Trim() + "=" + originalLine[i].Trim() + "&";
   }

outFileSw.WriteLine(newLine.Remove(newLine.Length - 1));
FileInfo fileInfo = new FileInfo(@outFile);

  if (fileInfo.Length > (1.3 * 1024.0 * 1024.0 * 1024.0)) // greater than 1.3 GB  
  {
          outFileSw.Close();
          outFileNumber = outFileNumber + 1;
          outFileSw = new System.IO.StreamWriter(@outFile + outFileNumber.ToString() + ".txt");
  }

}
outFileSw.Dispose();
sr.Close();
sr.Dispose();

Error details are :

Exception Message: The specified network name is no longer available.

Stack Trace:    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
 at System.IO.FileStream.WriteCore(Byte[] buffer, Int32 offset, Int32 count)
 at System.IO.FileStream.Write(Byte[] array, Int32 offset, Int32 count)
 at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)
 at System.IO.StreamWriter.Write(Char[] buffer, Int32 index, Int32 count)
 at System.IO.TextWriter.WriteLine(String value)
 at ExampleExe.ExampleProcess.FnFiles()


 Unhandled Exception: System.IO.IOException: The specified network name is no longer available.

 at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
 at System.IO.FileStream.WriteCore(Byte[] buffer, Int32 offset, Int32 count)
 at System.IO.FileStream.FlushWrite(Boolean calledFromFinalizer)
 at System.IO.FileStream.Dispose(Boolean disposing)
 at System.IO.FileStream.Finalize()

Thanks,
Kanu

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:28:00+00:00Added an answer on June 11, 2026 at 4:28 pm

    Here is a FileWriter with StringBuilder buffer. It uses a separate thread to write while main thread keeps accumulating data. Writes 10MB buffer at a time. Buffered data is kept in Queue object; writer thread removes item from this Queue and writes it out full using with File.AppendAllText method.

    private Queue<StringBuilder> writeQueue;
    private bool isComplete;
    
    public void FileWriter()
    {
        this.isComplete = false;
        this.writeQueue = new Queue<StringBuilder>();
    
        var writer = new Action<string>(this.StartWriting);
        var writerAsync = writer.BeginInvoke(@"outputfile.txt", null, null);
    
        using (StreamReader sr = new StreamReader(@"inputfile.txt"))
        {
            var fileHeaders = sr.ReadLine()
                .Split('\t')
                .Where(i => !string.IsNullOrEmpty(i))
                .Select(j => j.Trim())
                .ToList();
    
            var buffer = new StringBuilder();
            while (!sr.EndOfStream)
            {
                var originalLine = sr.ReadLine()
                    .Split('\t')
                    .Where(i => !string.IsNullOrEmpty(i))
                    .Select(j => j.Trim())
                    .ToList();
    
                var line = new StringBuilder();
                //Must have same number of items
                if (originalLine.Count == fileHeaders.Count)
                {
                    for (int i = 0; i < fileHeaders.Count(); i++)
                    {
                        line.AppendFormat("{0}={1}&", fileHeaders[i], originalLine[i]);
                    }
                    line.AppendLine();
                }
    
                buffer.AppendLine(line.ToString());
                if (buffer.Length > 1024 * 1024 * 10)//approx 10MB 
                {
                    lock (this.writeQueue)
                    {
                        this.writeQueue.Enqueue(buffer);
                    }
                    buffer = new StringBuilder();
                }
            }
            //Queue any final remaining data
            if (buffer.Length>0) lock (this.writeQueue)
            {
                this.writeQueue.Enqueue(buffer);
            }
        }
        this.isComplete = true;
        writer.EndInvoke(writerAsync);
    }
    
    private void StartWriting(string outFilePath)
    {
        while (!this.isComplete || this.writeQueue.Count > 0)
        {
            StringBuilder queuedItem;
            if (this.writeQueue.Count > 0)
            {
                lock (this.writeQueue)
                {
                    queuedItem = this.writeQueue.Dequeue();
                }
                File.AppendAllText(outFilePath, queuedItem.ToString());
            }
            System.Threading.Thread.Sleep(5000); //Sleep 5sec
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write text to my txt file. After the first write
I am trying to write a program called encrypt that transforms a text file
I'm trying to find a way to write to a text file from as2.
I am trying to write a bash script that will take a text file
I'm trying to read data from a text file, clear it, and then write
I am trying to write an output text file from table. I am not
I am trying to write program for parsing and processing text file. After not
I am trying to write some messages to a text file using Debug.WriteLine(Message). Here
I am trying to write some simple code which will read a text file
Ultimately I'm trying to write out a range into a text file. When I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.