I have fileA and fileB and want to exchange them. (In computer programming, the act of swapping two variables refers to mutually exchanging the values of the variables)
File fileA, fileB, temp
fileA.renameTo(temp);
try {
fileB.renameTo(fileA);
try {
// process fileA, B
} finally {
fileA.renameTo(fileB);
}
} finally {
temp.renameTo(fileA);
}
I would avoid the temp file and pair of renames if there would be a "hardware" swap operation.