Skip to content

Commit 234107f

Browse files
timoteoponcepedja4
authored andcommitted
BAEL-1933 Added code examples and unit tests for creating symbolic links (eugenp#4608)
* BAEL-1933 Added code examples and unit tests for creating symbolic links * Fixed PMD violation on unit test naming * Fixed review comments * Applied baeldung-eclipse formatter settings
1 parent f06f061 commit 234107f

2 files changed

Lines changed: 90 additions & 0 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.baeldung.symlink;
2+
3+
import java.io.IOException;
4+
import java.nio.charset.StandardCharsets;
5+
import java.nio.file.DirectoryStream;
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
8+
import java.nio.file.Paths;
9+
import static java.nio.file.StandardOpenOption.*;
10+
import java.util.stream.IntStream;
11+
12+
public class SymLinkExample {
13+
14+
public void createSymbolicLink(Path link, Path target) throws IOException {
15+
if (Files.exists(link)) {
16+
Files.delete(link);
17+
}
18+
Files.createSymbolicLink(link, target);
19+
}
20+
21+
public void createHardLink(Path link, Path target) throws IOException {
22+
if (Files.exists(link)) {
23+
Files.delete(link);
24+
}
25+
Files.createLink(link, target);
26+
}
27+
28+
public Path createTextFile() throws IOException {
29+
byte[] content = IntStream.range(0, 10000)
30+
.mapToObj(i -> i + System.lineSeparator())
31+
.reduce("", String::concat)
32+
.getBytes(StandardCharsets.UTF_8);
33+
Path filePath = Paths.get(".", "target_link.txt");
34+
Files.write(filePath, content, CREATE, TRUNCATE_EXISTING);
35+
return filePath;
36+
}
37+
38+
public void printLinkFiles(Path path) throws IOException {
39+
try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
40+
for (Path file : stream) {
41+
if (Files.isDirectory(file)) {
42+
printLinkFiles(file);
43+
} else if (Files.isSymbolicLink(file)) {
44+
System.out.format("File link '%s' with target '%s'%n", file, Files.readSymbolicLink(file));
45+
}
46+
}
47+
}
48+
}
49+
50+
public static void main(String[] args) throws IOException {
51+
new SymLinkExample().printLinkFiles(Paths.get("."));
52+
}
53+
54+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.symlink;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.io.IOException;
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
8+
import java.nio.file.Paths;
9+
10+
import org.junit.Test;
11+
12+
public class SymLinkExampleUnitTest {
13+
14+
@Test
15+
public void whenUsingFiles_thenCreateSymbolicLink() throws IOException {
16+
SymLinkExample example = new SymLinkExample();
17+
Path filePath = example.createTextFile();
18+
Path linkPath = Paths.get(".", "symbolic_link.txt");
19+
example.createSymbolicLink(linkPath, filePath);
20+
assertTrue(Files.isSymbolicLink(linkPath));
21+
}
22+
23+
@Test
24+
public void whenUsingFiles_thenCreateHardLink() throws IOException {
25+
SymLinkExample example = new SymLinkExample();
26+
Path filePath = example.createTextFile();
27+
Path linkPath = Paths.get(".", "hard_link.txt");
28+
example.createHardLink(linkPath, filePath);
29+
assertFalse(Files.isSymbolicLink(linkPath));
30+
assertEquals(filePath.toFile()
31+
.length(),
32+
linkPath.toFile()
33+
.length());
34+
}
35+
36+
}

0 commit comments

Comments
 (0)