Is file a normal file

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/is-regular-file.txt";

Path source;

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

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

Straight up Java

File.isFile

@Test
public void file_is_regular_file_java () {
    
    File file = source.toFile();
    
    boolean isRegularFile = file.isFile();
    
    assertTrue(isRegularFile);
}

Java 7 File I/O

@Test
public void file_is_regular_file_nio_files () throws IOException {
    
    boolean isRegularFile = Files.isRegularFile(source);
    
    assertTrue(isRegularFile);
}

BasicFileAttributes.isRegularFile

@Test
public void file_is_regular_file_nio() throws IOException {

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

    boolean isRegularFileWithOpaqueContent = attr.isRegularFile();

    assertTrue(isRegularFileWithOpaqueContent);
}