Fetching the name of the current working directory with Java 7+

Paths.get("").toAbsolutePath().getFileName() returns the name of the current directory:

    import java.nio.file.Path;
    import java.nio.file.Paths;
    import static org.hamcrest.CoreMatchers.is;
    import static org.junit.Assert.assertThat;
    import org.junit.Test;
    public class AppTest {
        public void currentFolderFolderName() {
            String expected = "[CURRENT WORKING DIRECTORY NAME]";
            Path currentPath = Paths.get("").toAbsolutePath().getFileName();
            assertThat(currentPath.toString(), is(expected));

If you launch the test in directory: "/duke/work/coolness", currentPath will yield coolness.

See you at Web, MicroProfile and Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home: airhacks.io.

NEW: Online Workhop Effective WebApps without Frameworks is also coming to: MUC Airport.

Airport MUC workshops: Web (SPA, PWAs, Offline, Desktop, Mobile) Applications Essentials and Effective Web Applications. No migrations. #usetheplatform

Podcast: airhacks.fm and newsletter: airhacks.news

A book about rethinking Java EE Patterns


Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
...the last 150 posts
...the last 10 comments