title: Re-Assembling a splitted file correctly
body: <p>Please have a look at the following code</p>
  
  <pre><code>package normal;
  
  import java.io.*;
  import java.util.*;
  
  public class ReGenerateFiles
  {
      private File inputFolder, outputFolder;
      private String outputFormat, nameOfTheFile;
  
      private FileInputStream fis;
      private FileOutputStream fos;
      List &lt;Byte&gt; al;
  
      private byte[] buffer;
  
      private int blockNumber = 1;
  
      public ReGenerateFiles(File inputFile, File outputFile, String nameOfTheFile, String outputFormat)
      {
          this.inputFolder = inputFile;
          this.outputFolder = outputFile;
          this.nameOfTheFile = nameOfTheFile;
          this.outputFormat = outputFormat;
      }
  
      public void reGenerate() throws IOException
      {
          File file[] = inputFolder.listFiles();
  
          for(int i=0;i&lt;file.length;i++)
          {
              System.out.println(file[i]);
          }
  
          for(int i=0;i&lt;file.length;i++)
          {
              try
              {
                  buffer = new byte[5000000];
  
  
                  int read = fis.read(buffer, 0, buffer.length);
                  fis.close();
  
                  writeBlock();
  
  
              }
              catch(IOException io)
              {
                  io.printStackTrace();
              }
              finally
              {
                  fis.close();
                  fos.close();
              }
          }
      }
  
      private void writeBlock()throws IOException
      {
          try
          {
              fos = new FileOutputStream(outputFolder+"/"+nameOfTheFile+"."+outputFormat,true);
                  fos.write(buffer, 0, buffer.length);
                  fos.flush();
                  fos.close();
          }
          catch(Exception e)
          {
              fos.close();
              e.printStackTrace();
          }
          finally
          {
              fos.close();
          }
      }
  
  
  }
  </code></pre>
  
  <p>In here, I am trying to re-assemble a <strong>splited</strong> file, back to its original file. <a href="https://stackoverflow.com/questions/10863522/java-reading-bytes-from-a-txt-file-line-by-line">This is how I split it</a> (The selected answer)</p>
  
  <p>Now, when I am trying to re-assemble it, I am getting an error saying something similar to "Cannot access kalimba.mp3. It is used by another program". This happens after executing 93 splitted files (there are 200 more). Why it is happening, even though I have make sure the streams are closed inside finally block?</p>
  
  <p>Another question, I have assigned 500000 as the byte array value. I did this because I failed to set the byte array according to the original size of the particular file which is about to process.  I assigned the byte array value as </p>
  
  <pre><code>buffer = new byte[(byte)file[i].length();
  </code></pre>
  
  <p>before, but it didn't work. </p>
  
  <p>Please help me to solve these two issues and re-assemble the splitted file back, correctly.</p>