Is file a directory

File meta data is stored within the file system and directories. It tracks information about each of the file which is typically referred to as file attributes. File attributes include size of a file, is it a directory, a symbolic link, a regular file, determine if the file is hidden, retrieve the last modified time and find when a file was created.

Setup

private static final String SOURCE = "com/levelup/java/io";

Path source;

@Before
public void setUp() throws IOException, URISyntaxException {

    source = Paths.get(this.getClass().getClassLoader().getResource(SOURCE)
            .toURI());
}

Straight up Java

File.isDirectory

@Test
public void file_is_directory_java () {
    
    File file = source.toFile();
    
    boolean isFileADirectory = file.isDirectory();
    
    assertTrue(isFileADirectory);   
}

Java 7 File I/O

@Test
public void file_is_directory_nio_files () {
    
    boolean isFileADirectory = Files.isDirectory(source);

    assertTrue(isFileADirectory);
}

BasicFileAttributes.isDirectory

@Test
public void file_is_directory_nio() throws IOException {

    BasicFileAttributes attr = Files.readAttributes(source,
            BasicFileAttributes.class);

    boolean isFileADirectory = attr.isDirectory();

    assertTrue(isFileADirectory);
}