To interact with the filesystem you use the methods of the class Files
.
To check the existence of the file or directory a path points to, you use the following methods:
Files.exists(Path path)
and
Files.notExists(Path path)
!Files.exists(path)
does not neccesarily have to be equal to Files.notExists(path)
, because there are three possible scenarios:
exists
returns true
and notExists
returns false
in this case)exists
returns false
and notExists
returns true
)exists
and nonExists
return false.This is done using Files.isDirectory(Path path)
and Files.isRegularFile(Path path)
Path p1 = Paths.get("/var/www");
Path p2 = Paths.get("/home/testuser/File.txt");
Files.isDirectory(p1) == true
Files.isRegularFile(p1) == false
Files.isDirectory(p2) == false
Files.isRegularFile(p2) == true
This can be done using the following methods:
Files.isReadable(Path path)
Files.isWritable(Path path)
Files.isExecutable(Path path)
Files.isHidden(Path path)
Files.isSymbolicLink(Path path)
Files.probeContentType(Path path)
This tries to get the MIME type of a file. It returns a MIME type String, like this:
text/plain
for text filestext/html
for HTML pagesapplication/pdf
for PDF filesimage/png
for PNG files