From c9599313c809abbbb96204b3c785fc9284d1f769 Mon Sep 17 00:00:00 2001 From: "sreekanth.nair" Date: Fri, 19 Jul 2019 11:46:05 +0530 Subject: [PATCH] Get Current Directory Using Java --- .../core/pwd/CurrentDirectoryFetcher.java | 40 +++++++++++++++++++ .../core/pwd/CurrentDirectoryFetcherTest.java | 34 ++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 core-java-modules/core-java-os/src/main/java/com/baeldung/core/pwd/CurrentDirectoryFetcher.java create mode 100644 core-java-modules/core-java-os/src/test/java/com/baeldung/core/pwd/CurrentDirectoryFetcherTest.java diff --git a/core-java-modules/core-java-os/src/main/java/com/baeldung/core/pwd/CurrentDirectoryFetcher.java b/core-java-modules/core-java-os/src/main/java/com/baeldung/core/pwd/CurrentDirectoryFetcher.java new file mode 100644 index 0000000000..8f022d2cc7 --- /dev/null +++ b/core-java-modules/core-java-os/src/main/java/com/baeldung/core/pwd/CurrentDirectoryFetcher.java @@ -0,0 +1,40 @@ +package com.baeldung.core.pwd; + +import java.io.File; +import java.nio.file.FileSystems; +import java.nio.file.Paths; + +public final class CurrentDirectoryFetcher { + + public static void main(String[] args) { + System.out.printf("Current Directory Using Java System API: %s%n", currentDirectoryUsingSystemProperties()); + + System.out.printf("Current Directory Using Java IO File API: %s%n", currentDirectoryUsingFile()); + + System.out.printf("Current Directory Using Java NIO FileSystems API: %s%n", currentDirectoryUsingFileSystems()); + + System.out.printf("Current Directory Using Java NIO Paths API: %s%n", currentDirectoryUsingPaths()); + } + + public static String currentDirectoryUsingSystemProperties() { + return System.getProperty("user.dir"); + } + + public static String currentDirectoryUsingPaths() { + return Paths.get("") + .toAbsolutePath() + .toString(); + } + + public static String currentDirectoryUsingFileSystems() { + return FileSystems.getDefault() + .getPath("") + .toAbsolutePath() + .toString(); + } + + public static String currentDirectoryUsingFile() { + return new File("").getAbsolutePath(); + } + +} diff --git a/core-java-modules/core-java-os/src/test/java/com/baeldung/core/pwd/CurrentDirectoryFetcherTest.java b/core-java-modules/core-java-os/src/test/java/com/baeldung/core/pwd/CurrentDirectoryFetcherTest.java new file mode 100644 index 0000000000..dbaad211d9 --- /dev/null +++ b/core-java-modules/core-java-os/src/test/java/com/baeldung/core/pwd/CurrentDirectoryFetcherTest.java @@ -0,0 +1,34 @@ +package com.baeldung.core.pwd; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class CurrentDirectoryFetcherTest { + + private static final String CURRENT_DIR = "core-java-os"; + + @Test + public void whenUsingSystemProperties_thenReturnCurrentDirectory() { + assertTrue(CurrentDirectoryFetcher.currentDirectoryUsingSystemProperties() + .endsWith(CURRENT_DIR)); + } + + @Test + public void whenUsingJavaNioPaths_thenReturnCurrentDirectory() { + assertTrue(CurrentDirectoryFetcher.currentDirectoryUsingPaths() + .endsWith(CURRENT_DIR)); + } + + @Test + public void whenUsingJavaNioFileSystems_thenReturnCurrentDirectory() { + assertTrue(CurrentDirectoryFetcher.currentDirectoryUsingFileSystems() + .endsWith(CURRENT_DIR)); + } + + @Test + public void whenUsingJavaIoFile_thenReturnCurrentDirectory() { + assertTrue(CurrentDirectoryFetcher.currentDirectoryUsingFile() + .endsWith(CURRENT_DIR)); + } +} \ No newline at end of file