Skip to content

Commit 4ffc56f

Browse files
committed
Use Files.move instead of File.renameTo
1 parent 7687e62 commit 4ffc56f

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

core/src/main/java/com/google/googlejavaformat/java/FormatFileCallable.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import java.io.PrintWriter;
3535
import java.io.Writer;
3636
import java.nio.charset.StandardCharsets;
37+
import java.nio.file.Files;
38+
import java.nio.file.StandardCopyOption;
3739
import java.util.ArrayList;
3840
import java.util.List;
3941
import java.util.concurrent.Callable;
@@ -232,9 +234,19 @@ private boolean writeOutput(Write write) {
232234
}
233235
return false;
234236
}
235-
if (!new File(tempFileName).renameTo(new File(fileToFormat.fileName()))) {
237+
try {
238+
Files.move(
239+
new File(tempFileName).toPath(),
240+
new File(fileToFormat.fileName()).toPath(),
241+
StandardCopyOption.REPLACE_EXISTING);
242+
} catch(IOException e) {
236243
synchronized (outputLock) {
237-
errWriter.append(tempFileName).append(": cannot rename temp file").append('\n').flush();
244+
errWriter
245+
.append(tempFileName)
246+
.append(": cannot rename temp file: ")
247+
.append(e.getMessage())
248+
.append('\n')
249+
.flush();
238250
}
239251
return false;
240252
}

0 commit comments

Comments
 (0)