In JDK7, is there a way to use the ternary operator when moving a file to conditionally use REPLACE_EXISTING?

I don't like how the code below looks, and I would like to know how to do this using the ternary operator:

if (isIndexed) {
    Files.move(source, destination);
}
else {
    Files.move(source, destination, StandardCopyOption.REPLACE_EXISTING);
}

I expect something similar:

Files.move(source, destination, isIndexed ? xxxx : StandardCopyOption.REPLACE_EXISTING);

If I could use some kind of "default" option, I would think that this would be what I'm looking for. But the enumeration for StandardCopyOption does not have the "NONE" option.

So I probably missed something. What is it?

+3
source share
1 answer

This is a varargs argument, so you can just set an empty array new StandardCopyOption[0].

Files.move(source, destination, isIndexed ? new StandardCopyOption[0] : new StandardCopyOption[] { StandardCopyOption.REPLACE_EXISTING });

. () , .

+6

All Articles