The class java.io.File
can be used to check for files or directory using the following methods:
- isFile
- isDirectorory
You may also be interested in exists() to check first if the file or directory exists.
[wp_ad_camp_1]
Let’s say we have these files.
We can easily check the facts with these sample codes.
[wp_ad_camp_2]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | package com.karlsangabriel.io; import java.io.File; public class FileOrDirectoryExample { public static void main(String[] args) { File aFile = new File("C:/somewhere/FileOrDirectoryExampleTestData/file.txt"); File aDirectory = new File("C:/somewhere/FileOrDirectoryExampleTestData/directory"); System.out.println("aFile is a file? " + aFile.isFile()); System.out.println("aDirectory is a direcory? " + aDirectory.isDirectory()); } } |
This outputs
1 2 | aFile is a file? true aDirectory is a direcory? true |
[wp_ad_camp_3]