0

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.

Val
  • 1
  • 8
  • 40
  • 64
  • 1
    "hardware swap operation." Files are not handled by hardware. The hardware just sees the disk as a bunch of sectors. – nanofarad Sep 17 '13 at 19:59
  • 2
    Well, theoretically, you _could_ do the ol' `XOR` trick, by keeping the file lengths (if they are different) in mind, but I have doubts about the practical usage, not to mention, the possibility of implementation... Also, it would be slow as hell, compared to renaming one to a temp name... – ppeterka Sep 17 '13 at 20:01
  • @ppeterka66 Teaching askers about hypothetical and risky, though mathematically proper operations may not be the best idea... – nanofarad Sep 17 '13 at 20:03
  • @hexafraction I think the OP already heard about it (check the wiki link), so I felt I have to bust that myth for this case. – ppeterka Sep 17 '13 at 20:09
  • @ppeterka66 3 XOR operations per swap. With temp, two ops suffices. So, is your solution simpler? XORing a file must be much more simpler if more operations is simpler. How do you xor a file? – Val Sep 17 '13 at 20:09
  • @Val Did I say simpler or quicker? Where? (also, I can XOR just about anything composed of bytes, and you can too) – ppeterka Sep 17 '13 at 20:12
  • Why did I say simpler or quicker? Do I say that I am an idiot who wants to overcomplicate my life? In my question I clearly say that I want to simplify the routine operation. Why do you put upside down and represent my query like a request to overcomplicate my code? – Val Sep 17 '13 at 20:15
  • @Val Please calm down. That was a hypothetical, mathematically *valid* approach. – nanofarad Sep 17 '13 at 20:20
  • Why not to use a knife as a screwdriver? Start with the statement that water is liquid. This statement is not less scieticically valid and as much as irrelevant. There is 100500 valid approaches in the world. It does not mean that the purpose of this question is to apply them all. – Val Sep 18 '13 at 10:29

1 Answers1

1

In general there is no concept of an 'atomic' file-swap operation. Even if there were, it would be file-system dependant. I don't believe the 'common' filesystems (FAT*, ext*, NTFS, etc.) support an atomic swap operation.

Certainly, the atomic operation is not available through Java. Even if it were, it could never work accross file-systems, etc.

What you are looking for is the type of thing that would not find a home in Java simply because it could never be assured on the various supported platforms that Java runs on.

Using temp with some file locking is about the only way to get a relatively assured swap operation.

rolfl
  • 17,539
  • 7
  • 42
  • 76
  • Well, I do not require the atomic operation. I am happy if it is composite on the background. – Val Sep 17 '13 at 20:11
  • If you are OK with it bein composite in the background, why not just make your code a method, and then call it? – rolfl Sep 17 '13 at 20:14
  • I could implement renameTo myself also. I believe swapping files is a routine operation. Am I wrong? – Val Sep 17 '13 at 20:17
  • You could use a smart mechanism for your temp file, e.g. `File temp = File.createTempFile("temp", "tmp", filea.getParentDir())` and then you don't need to specify the temp file manually – rolfl Sep 17 '13 at 20:17