I want to move some file to other place, using Java SE 1.6
So, I have two files:
File srcFile, dstFile;
When I want to move file srcFile to the place of dstFile:
boolean result = srcFile.renameTo(dstFile);
But there some errors can occur, and result will be false. In this case I want to ask user if he wants to repeat this operation. Which is the best way to do this? I would use a goto statement here, but Java doesn’t have it.
Currently, I am doing it in this way:
boolean retry = false;
do {
if (!srcFile.renameTo(dstFile)) {
// TODO: Error during file replacement
Object[] retryOptions = { "Yes", "Retry" };
replaceFileOption = JOptionPane.showOptionDialog(frame, "There was a error durring moving file" + srcFile.getPath() + ". Skip this file?", "Warning", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null,
retryOptions, retryOptions[0]);
if (replaceFileOption == 2)
retry = true;
}
} while (retry);
Is there another way, which will not be using while() cycles?
When your goal was to avoid an extra var, do it this way