title: How is the write(byte[],int,int) method working?
body: <pre><code>public class JavaCopyFileProgram {
  
  
  
          public static void main(String[] args)
          {    
              File sourceFile = new File("F:/Study/Java/Java Programs/Factory Methods.txt");
  
              File destFile = new File("D:/DestFile.txt");
  
              FileInputStream inStream = null;
  
              FileOutputStream outStream = null;
  
              try
              {
                  inStream = new FileInputStream(sourceFile);
  
                  outStream = new FileOutputStream(destFile);
  
                  byte[] buffer = new byte[1024];
  
                  int length;
  
                  while ((length = inStream.read(buffer)) != -1) 
                  { 
                      outStream.write(buffer, 0, length);
                  }
              }
              catch (IOException e)
              {
                  e.printStackTrace();
              }
              finally
              {
                  try
                  {
                      inStream.close();
  
                      outStream.close();
                  }
                  catch (IOException e) 
                  {
                      e.printStackTrace();
                  }
              }
  
              System.out.println("Success");
          }
      }   
  </code></pre>
  
  <p>I am not able to understand how is thw write() method working? When it is first invoked then it is writing the cotents from 0 index to length of the byte array but when it is invoked second time then how is it appending the new text to the end of the previous text? It should override the previous content as again write is called with 0 as starting index. Please help me if i am understood something wrong?</p>