Use Java 7 Files to delete and rename.

This commit is contained in:
Peter Lawrey
2015-05-20 17:53:17 +01:00
parent db3dcbf9bf
commit 56c3fbc5fc
2 changed files with 14 additions and 5 deletions
+11 -4
View File
@@ -203,12 +203,19 @@ public class FilePerKeyMap implements Map<String, String>, Closeable {
} catch (IOException e) {
throw new AssertionError(e);
}
tmpFile.renameTo(file);
try {
Files.move(tmpFile.toPath(), file.toPath(), StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
private void deleteFile(Path path) {
File key = path.toFile();
key.delete();
try {
Files.deleteIfExists(path);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
public void close() {
@@ -297,7 +304,7 @@ public class FilePerKeyMap implements Map<String, String>, Closeable {
continue;
}
if (kind == StandardWatchEventKinds.ENTRY_CREATE) {
if (kind == StandardWatchEventKinds.ENTRY_CREATE) {
Path p = dirPath.resolve(fileName);
try {
String mapVal = getFileContents0(p);
+3 -1
View File
@@ -31,13 +31,15 @@ public class FilePerKeyMapTest {
public void testMapMethods() throws InterruptedException {
String val = map.put("one", "test1");
assertEquals(val, null);
Thread.sleep(5);
Thread.sleep(10);
//After the entry has been written the value returned should be
//the previous value
val = map.put("one", "test2");
assertEquals(val, "test1");
Thread.sleep(10);
//Check that get returns the latest value
val = map.get("one");
assertEquals("test2", val);