BAEL-1933 Added code examples and unit tests for creating symbolic links (#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
This commit is contained in:
parent
f06f061075
commit
234107f519
|
@ -0,0 +1,54 @@
|
|||
package com.baeldung.symlink;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.DirectoryStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import static java.nio.file.StandardOpenOption.*;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class SymLinkExample {
|
||||
|
||||
public void createSymbolicLink(Path link, Path target) throws IOException {
|
||||
if (Files.exists(link)) {
|
||||
Files.delete(link);
|
||||
}
|
||||
Files.createSymbolicLink(link, target);
|
||||
}
|
||||
|
||||
public void createHardLink(Path link, Path target) throws IOException {
|
||||
if (Files.exists(link)) {
|
||||
Files.delete(link);
|
||||
}
|
||||
Files.createLink(link, target);
|
||||
}
|
||||
|
||||
public Path createTextFile() throws IOException {
|
||||
byte[] content = IntStream.range(0, 10000)
|
||||
.mapToObj(i -> i + System.lineSeparator())
|
||||
.reduce("", String::concat)
|
||||
.getBytes(StandardCharsets.UTF_8);
|
||||
Path filePath = Paths.get(".", "target_link.txt");
|
||||
Files.write(filePath, content, CREATE, TRUNCATE_EXISTING);
|
||||
return filePath;
|
||||
}
|
||||
|
||||
public void printLinkFiles(Path path) throws IOException {
|
||||
try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
|
||||
for (Path file : stream) {
|
||||
if (Files.isDirectory(file)) {
|
||||
printLinkFiles(file);
|
||||
} else if (Files.isSymbolicLink(file)) {
|
||||
System.out.format("File link '%s' with target '%s'%n", file, Files.readSymbolicLink(file));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
new SymLinkExample().printLinkFiles(Paths.get("."));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package com.baeldung.symlink;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class SymLinkExampleUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenUsingFiles_thenCreateSymbolicLink() throws IOException {
|
||||
SymLinkExample example = new SymLinkExample();
|
||||
Path filePath = example.createTextFile();
|
||||
Path linkPath = Paths.get(".", "symbolic_link.txt");
|
||||
example.createSymbolicLink(linkPath, filePath);
|
||||
assertTrue(Files.isSymbolicLink(linkPath));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingFiles_thenCreateHardLink() throws IOException {
|
||||
SymLinkExample example = new SymLinkExample();
|
||||
Path filePath = example.createTextFile();
|
||||
Path linkPath = Paths.get(".", "hard_link.txt");
|
||||
example.createHardLink(linkPath, filePath);
|
||||
assertFalse(Files.isSymbolicLink(linkPath));
|
||||
assertEquals(filePath.toFile()
|
||||
.length(),
|
||||
linkPath.toFile()
|
||||
.length());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue