How to get some information about Path object?

The java.nio.Path provides some methods to obtain information about the Path. For example, you can get information about the file name, the parent and the root path. For these you can call the getFileName(), getParent() and getRoot() method respectively.

You can also get the number of elements that make up this Path using the getNameCount() method. And to get the sub-path you can use the subpath() method and specify the starting and ending indexes. The code snippet below demonstrate to you how to get this information.

package org.kodejava.io;

import java.nio.file.Path;
import java.nio.file.Paths;

public class PathInfoExample {
    public static void main(String[] args) {
        // Create a Path for Windows notepad program.
        Path notepad = Paths.get("C:/Windows/System32/notepad.exe");

        // Get some information about the Path object.
        System.out.printf("File name         : %1$s%n", notepad.getFileName());
        System.out.printf("Name count        : %1$s%n", notepad.getNameCount());
        System.out.printf("Parent path       : %1$s%n", notepad.getParent());
        System.out.printf("Root path         : %1$s%n", notepad.getRoot());
        System.out.printf("Sub path from root: %1$s%n", notepad.subpath(0, 2));
    }
}

This code will print something like:

File name         : notepad.exe
Name count        : 3
Parent path       : C:\Windows\System32
Root path         : C:\
Sub path from root: Windows\System32

How do I create a java.nio.Path?

The following code snippet show you how to create a Path. A Path (java.nio.Path) in an interface that represent a location in a file system, such as C:/Windows/System32 or /usr/bin.

To create a Path we can use the java.nio.Paths.get(String first, String... more) methods. Below you can see how to create a Path by passing only the first string and by passing a first string plus some varargs string.

package org.kodejava.io;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class PathCreate {
    public static void main(String[] args) {
        // Create a Path that represents Windows installation location.
        Path windows = Paths.get("C:/Windows");

        // Check to see if the path represent a directory.
        if (Files.isDirectory(windows)) {
            // do something
        }

        // Create a Path that represent Windows programs installation location.
        Path programFiles = Paths.get("C:/Program Files");
        Files.isDirectory(programFiles);

        // Create a Path that represent the notepad.exe program
        Path notepad = Paths.get("C:/Windows", "System32", "notepad.exe");

        // Check to see if the path represent an executable file.
        if (Files.isExecutable(notepad)) {
            // do something
        }
    }
}

How do I write a text file in JDK 7?

In JDK 7 we can write all lines from a List of String into a file using the Files.write() method. We need to provide the Path of the file we want to write to, the List of strings and the charsets. Each line is a char sequence and is written to the file in sequence with each line terminated by the platform’s line separator.

Let’s see the code snippet below:

package org.kodejava.io;

import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class WriteTextFile {
    public static void main(String[] args) {
        Path file = Paths.get("D:/resources/data.txt");

        List<String> lines = new ArrayList<>();
        lines.add("Lorem Ipsum is simply dummy text of the printing ");
        lines.add("and typesetting industry. Lorem Ipsum has been the ");
        lines.add("industry's standard dummy text ever since the 1500s, ");
        lines.add("when an unknown printer took a galley of type and ");
        lines.add("scrambled it to make a type specimen book.");

        try {
            // Write lines of text to a file.
            Files.write(file, lines, StandardCharsets.UTF_8);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

This code snippet will create a file called data.txt under the resources folder. Please make sure that this folder is existed before you tried to run the code.

How do I get the absolute path of a file in Java?

The following code snippet shows you how to get the absolute path of a file. To do this we use the java.io.File object getAbsolutePath() method.

package org.kodejava.io;

import java.io.File;

public class AbsolutePathExample {
    public static void main(String[] args) {
        // Create an instance of File called file.
        File file = new File("README.md");

        // Now we want to know where is exactly this file is located in our file 
        // system. To do this we can use the getAbsolutePath() method of the File
        // class.
        String absolutePath = file.getAbsolutePath();

        // Print out the JavaProgrammingBook.pdf location to the console.
        System.out.println("AbsolutePath = " + absolutePath);
    }
}

Here is the result of the program:

AbsolutePath = F:\Wayan\Kodejava\kodejava-example\README.md

How do I get path / classpath separator?

OS platform has a different symbol used for path separator. Path separator is a symbol that separate one path element from the other. In Windows the path separator is a semicolon symbol (;), you have something like:

.;something.jar;D:/libs/commons.jar

While in Linux based operating systems the path separator is a colon symbol (:), it looks like:

.:something.jar:/libs/commons.jar

To obtain the path separator you can use the following code.

package org.kodejava.lang;

import java.util.Properties;

public class PathSeparator {
    public static void main(String[] args) {
        // Get System properties
        Properties properties = System.getProperties();

        // Get the path separator which is unfortunately
        // using a different symbol in different OS platform.
        String pathSeparator = properties.getProperty("path.separator");
        System.out.println("pathSeparator = " + pathSeparator);
    }
}