title: Java - Write content from one file chunk by chunk (e.g. 8 Bytes) alternately into multiple files
body: <p>So I've been trying to read the content of a text file and write the content chunk by chunk alternately into e.g. 2 new files.</p>
  <p>I already tried multiple ways to do that but it won't work (OutputStream and FileOutputStream seems to be the most suitable).</p>
  <p>Before i tried to part the file in e.g. 3 Parts and wrote the first part in one file, the second part in another and so on. Which worked perfectly fine with OutputStream and FileOutputStream.</p>
  <p>But it won't work when i want to do it alternately.</p>
  <p>To do it alternately i use the round robin algorithm, which on its own works fine.</p>
  <p>I would be really thankful if you could show me some examples to do it!</p>
  <pre><code>public void splitFile(String filePath, int numberOfParts, long sizeOfParts[]) throws FileNotFoundException, IOException, SQLException {
      long bytes = 8;
      
      OutputStream partsPath[] = new OutputStream[numberOfParts];
      long bytePositition[] = new long[numberOfParts];
  
      long copy_size[] = new long[numberOfParts];
      for (int i = 0; i &lt; numberOfParts; i++) {
          copy_size[i] = sizeOfParts[i];
          partsPath[i] = new FileOutputStream(path); //Gets Path from my Database (works)
          //System.out.println(cloudsTable.getCloudsPathsFromDatabase(i) + '\\' + name + (i + 1) + fileType);
      }
  
      InputStream file = new FileInputStream(filePath);
  
      while (true) {
          boolean done = true;
  
          for (int i = 0; i &lt; numberOfParts; i++) {
  
              if (copy_size[i] &gt; 0) {
                  done = false;
                  if (copy_size[i] &gt; bytes) {
                      copy_size[i] -= bytes;
                      bytePositition[i] += bytes;
                      
                      System.out.println(&quot;file &quot; + i + &quot; &quot; + bytePositition[i]);
                      readWrite(file, bytePositition[i], partsPath[i]);
                  } else {
                      bytePositition[i] += copy_size[i];
                      
                      System.out.println(&quot;rest file &quot; + i + &quot; &quot; + bytePositition[i]);
  
                      readWrite(file, bytePositition[i], partsPath[i]);
                      copy_size[i] = 0;
                  }
              }
  
          }
  
  
          if (done == true) {
  
              break;
          }
  
      }
      file.close();
      for (int i = 0; i &lt; partsPath.length; i++) {
          partsPath[i].close();
      }
  
  }
  
  private void readWrite(InputStream file, long bytes, OutputStream path) throws IOException {
      byte[] buf = new byte[(int) bytes];
      while (file.read(buf) != -1) {
          path.write(buf);
          path.flush();
      }
  </code></pre>
  <p>}</p>
  <p>What the code does is, it only write the content of the Originalfile in the first-copied file and the following files are empty</p>
  <p>EDIT:
  To clarify what the code should do is write the first 8 bytes to go to file 1, second 8 bytes to go to file 2, third 8 bytes to go to file 3, fourth 8 bytes to go to file 1, and so on, round robin, until file 1 is sizeOfParts[0] long, file 2 is sizeOfParts[1] long, and file 3 is sizeOfParts[2] long.</p>