title: Writing a file using multiple threads
body: <p>I am trying to write a single huge file in Java using multiple threads. </p>
  
  <p>I have tried both <code>FileWriter</code> and <code>bufferedWriter</code> classes in Java.</p>
  
  <p>The content being written is actually an entire table (Postgres) being read using <code>CopyManager</code> and written. Each line in the file is a single tuple from the table and I am writing 100s of lines at a time.</p>
  
  <p>Approach to write:</p>
  
  <p>The single to-be-written file is opened by multiple threads in append mode.
  Each thread thereafter tries writing to the file file.</p>
  
  <p>Following are the issues I face:</p>
  
  <ul>
  <li>Once a while, the contents of the file gets overwritten i.e: One line remains incomplete and the next line starts from there itself. My assumption here is that the buffers for writer are getting full. This forces the writer to immediately write the data onto the file. The data written may not be a complete line and before it can write the remainder, the next thread writes its content onto the file. </li>
  <li>While using <code>Filewriter</code>, once a while I see a single black line in the file.</li>
  </ul>
  
  <p>Any suggestions, how to avoid this data integrity issue?</p>