mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2025-03-10 09:20:04 +01:00
Delete the wallet file before renaming its replacement on Windows. Resolves issue 200.
This commit is contained in:
parent
e0ca3f4a7e
commit
4e4abf8a54
1 changed files with 12 additions and 8 deletions
|
@ -186,22 +186,26 @@ public class Wallet implements Serializable {
|
|||
*/
|
||||
public synchronized void saveToFile(File f) throws IOException {
|
||||
FileOutputStream stream = null;
|
||||
File temp = null;
|
||||
File temp;
|
||||
try {
|
||||
File directory = f.getAbsoluteFile().getParentFile();
|
||||
temp = File.createTempFile("wallet", null, directory);
|
||||
stream = new FileOutputStream(temp);
|
||||
saveToFileStream(stream);
|
||||
// Attempt to force the bits to hit the disk. In reality the OS or hard disk itself may still decide
|
||||
// to not write through to physical media for at least a few seconds, but this is the best we can do.
|
||||
stream.flush();
|
||||
stream.getFD().sync();
|
||||
if (!temp.renameTo(f)) {
|
||||
// Work around an issue on Windows whereby you can't rename over existing files.
|
||||
if (System.getProperty("os.name").toLowerCase().indexOf("win") >= 0) {
|
||||
if (f.delete() && temp.renameTo(f)) return; // else fall through.
|
||||
}
|
||||
throw new IOException("Failed to rename " + temp + " to " + f);
|
||||
}
|
||||
} finally {
|
||||
if (stream != null) {
|
||||
// Attempt to force the bits to hit the disk. In reality the OS or hard disk itself may still decide
|
||||
// to not write through to physical media for at least a few seconds, but this is the best we can do.
|
||||
stream.flush();
|
||||
stream.getFD().sync();
|
||||
stream.close();
|
||||
if (!temp.renameTo(f)) {
|
||||
throw new IOException("Failed to rename " + temp + " to " + f);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue