Java-Writing huge file using Byteoutputstream
<p>I am trying to write a file of size in between 1kb to 10GB using <code>ByteArrayOutputStream</code> but the below exception is thrown. I am using jdk 6. Please suggest any better high performance Api. I am using same network box to read and write.  </p>

<pre><code>Exception in thread "main" java.lang.OutOfMemoryError:   Requested array size exceeds VM limit
        at java.util.Arrays.copyOf(Unknown Source)
        at java.io.ByteArrayOutputStream.grow(Unknown Source)
        at java.io.ByteArrayOutputStream.ensureCapacity(Unknown Source)
        at java.io.ByteArrayOutputStream.write(Unknown Source)
        at java.io.OutputStream.write(Unknown Source)
        at
</code></pre>

<p>Code:</p>

<pre><code>import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;


public class PrepareFile {

    /**
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub

        new PrepareFile().constructFile("f:\\hello","f:\\output",10000000);

    }

    //Writes a large file of 10 GB using input file data of small size by duplicating
    public void constructFile(String fileName, String outPath, int multiplier) throws Exception {
        BufferedOutputStream fos = null;
        FileInputStream fis = null;

        final File inputFile = new File(fileName);
        String path = inputFile.getParent();
        if (outPath != null &amp;&amp; !outPath.isEmpty()) {
            path = outPath;
        }

        fis = new FileInputStream(fileName);

        try {



            // read the transactions in the input file.
            byte[] txnData = new byte[(int) inputFile.length()];
            fis.read(txnData);

            final File outFile = new File(path, "Myfile");
            fos = new BufferedOutputStream(new FileOutputStream(outFile));
            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
            final ByteArrayOutputStream baos1 = new ByteArrayOutputStream();

            //multiplier if input file size is 1 KB and output file is 10 GB, then multiplier value is (1024*1024)

            for (long i = 1; i &lt;= multiplier; i++) {

                if(i &gt;=40000 &amp;&amp; i % 40000==0){
                    System.out.println("i value now: "+i);
                    baos.writeTo(fos);
                    baos.reset();
                    //baos.write(txnData);
                }

                // write transactions
                baos.write(txnData);
                baos1.write(txnData); //Exception is coming at this line
            }

            int Padding = myCustomMethod(baos1.toByteArray());

            // write all out data to the output stream
            baos.writeTo(fos);

            baos.flush();
            baos1.flush();
        } catch(Exception e){
            e.printStackTrace();
        }finally {
            fos.close();
            fis.close();
        }

    }

    public int myCustomMethod(byte[] b){

        //Need complete bytes to prepare the file trailer
        return 0;
    }


}
</code></pre>