Skip to content

Commit 95bf16a

Browse files
BAEL-4391: Check if a file exists in Java (eugenp#9748)
* Check if a file exists in Java * Simple Assertions
1 parent 77326c1 commit 95bf16a

2 files changed

Lines changed: 93 additions & 0 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test-link*
2+
0.*
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.baeldung.existence;
2+
3+
import org.junit.Test;
4+
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.nio.file.Files;
8+
import java.nio.file.LinkOption;
9+
import java.nio.file.Path;
10+
import java.nio.file.Paths;
11+
import java.util.concurrent.ThreadLocalRandom;
12+
13+
import static org.junit.Assert.assertFalse;
14+
import static org.junit.Assert.assertTrue;
15+
16+
public class ExistenceUnitTest {
17+
18+
@Test
19+
public void givenFile_whenDoesNotExist_thenFilesReturnsFalse() {
20+
Path path = Paths.get("does-not-exist.txt");
21+
22+
assertFalse(Files.exists(path));
23+
assertTrue(Files.notExists(path));
24+
}
25+
26+
@Test
27+
public void givenFile_whenExists_thenFilesShouldReturnTrue() throws IOException {
28+
Path tempFile = Files.createTempFile("baeldung", "exist-nio");
29+
assertTrue(Files.exists(tempFile));
30+
assertFalse(Files.notExists(tempFile));
31+
32+
Path tempDirectory = Files.createTempDirectory("baeldung-exists");
33+
assertTrue(Files.exists(tempDirectory));
34+
assertFalse(Files.notExists(tempDirectory));
35+
36+
assertTrue(Files.isDirectory(tempDirectory));
37+
assertFalse(Files.isDirectory(tempFile));
38+
assertTrue(Files.isRegularFile(tempFile));
39+
40+
assertTrue(Files.isReadable(tempFile));
41+
42+
Files.deleteIfExists(tempFile);
43+
Files.deleteIfExists(tempDirectory);
44+
}
45+
46+
@Test
47+
public void givenSymbolicLink_whenTargetDoesNotExists_thenFollowOrNotBasedOnTheOptions() throws IOException {
48+
Path target = Files.createTempFile("baeldung", "target");
49+
Path symbol = Paths.get("test-link-" + ThreadLocalRandom.current().nextInt());
50+
51+
Path symbolicLink = Files.createSymbolicLink(symbol, target);
52+
assertTrue(Files.exists(symbolicLink));
53+
assertTrue(Files.isSymbolicLink(symbolicLink));
54+
assertFalse(Files.isSymbolicLink(target));
55+
56+
Files.deleteIfExists(target);
57+
assertFalse(Files.exists(symbolicLink));
58+
assertTrue(Files.exists(symbolicLink, LinkOption.NOFOLLOW_LINKS));
59+
60+
Files.deleteIfExists(symbolicLink);
61+
}
62+
63+
@Test
64+
public void givenFile_whenDoesNotExist_thenFileReturnsFalse() {
65+
assertFalse(new File("invalid").exists());
66+
assertFalse(new File("invalid").isFile());
67+
}
68+
69+
@Test
70+
public void givenFile_whenExist_thenShouldReturnTrue() throws IOException {
71+
Path tempFilePath = Files.createTempFile("baeldung", "exist-io");
72+
Path tempDirectoryPath = Files.createTempDirectory("baeldung-exists-io");
73+
74+
File tempFile = new File(tempFilePath.toString());
75+
File tempDirectory = new File(tempDirectoryPath.toString());
76+
77+
assertTrue(tempFile.exists());
78+
assertTrue(tempDirectory.exists());
79+
80+
assertTrue(tempFile.isFile());
81+
assertFalse(tempDirectory.isFile());
82+
83+
assertTrue(tempDirectory.isDirectory());
84+
assertFalse(tempFile.isDirectory());
85+
86+
assertTrue(tempFile.canRead());
87+
88+
Files.deleteIfExists(tempFilePath);
89+
Files.deleteIfExists(tempDirectoryPath);
90+
}
91+
}

0 commit comments

Comments
 (0)