title: Swaping two files on disk
body: <p>I have fileA and fileB and want to exchange them. (<a href="https://en.wikipedia.org/wiki/Swap_%28computer_science%29" rel="nofollow">In computer programming, the act of swapping two variables refers to mutually exchanging the values of the variables</a>)</p>
  
  <pre><code>File fileA, fileB, temp
  fileA.renameTo(temp);
  try {
      fileB.renameTo(fileA);
      try {
        // process fileA, B
      } finally {
          fileA.renameTo(fileB);
      } 
  } finally {
      temp.renameTo(fileA);
  }
  </code></pre>
  
  <p>I would avoid the temp file and pair of renames if there would be a "hardware" swap operation.</p>