mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2025-03-10 17:26:28 +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 {
|
public synchronized void saveToFile(File f) throws IOException {
|
||||||
FileOutputStream stream = null;
|
FileOutputStream stream = null;
|
||||||
File temp = null;
|
File temp;
|
||||||
try {
|
try {
|
||||||
File directory = f.getAbsoluteFile().getParentFile();
|
File directory = f.getAbsoluteFile().getParentFile();
|
||||||
temp = File.createTempFile("wallet", null, directory);
|
temp = File.createTempFile("wallet", null, directory);
|
||||||
stream = new FileOutputStream(temp);
|
stream = new FileOutputStream(temp);
|
||||||
saveToFileStream(stream);
|
saveToFileStream(stream);
|
||||||
} finally {
|
|
||||||
if (stream != null) {
|
|
||||||
// Attempt to force the bits to hit the disk. In reality the OS or hard disk itself may still decide
|
// 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.
|
// to not write through to physical media for at least a few seconds, but this is the best we can do.
|
||||||
stream.flush();
|
stream.flush();
|
||||||
stream.getFD().sync();
|
stream.getFD().sync();
|
||||||
stream.close();
|
|
||||||
if (!temp.renameTo(f)) {
|
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);
|
throw new IOException("Failed to rename " + temp + " to " + f);
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
if (stream != null) {
|
||||||
|
stream.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue