Java IO & NIO - Files.isHidden() Examples

Code Snippets Java IO & NIO Java 


Class:

java.nio.file.Files

java.lang.Objectjava.lang.Objectjava.nio.file.Filesjava.nio.file.FilesLogicBig

Method:

public static boolean isHidden(Path path)
                        throws IOException

Checks whether or not a file is hidden.

Parameters:
path - the path to the file to test
Returns:
true if the file is considered hidden


Examples


package com.logicbig.example.files;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class IsHiddenExample {

public static void main(String... args) throws IOException {
Path tempFile = Files.createTempFile("test-file", ".txt");
boolean hidden = Files.isHidden(tempFile);
System.out.println("file: " + tempFile);
System.out.println("exits: " + Files.exists(tempFile));
System.out.println("hidden: " + hidden);
}
}

Output

file: C:\Users\Joe\AppData\Local\Temp\test-file41471039271856387.txt
exits: true
hidden: false




package com.logicbig.example.files;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.DosFileAttributeView;

public class IsHiddenExample2 {

public static void main(String... args) throws IOException {
Path tempFile = Files.createTempFile("test-file", ".txt");
//on windows
DosFileAttributeView dosFileAttributeView =
Files.getFileAttributeView(tempFile, DosFileAttributeView.class);
dosFileAttributeView.setHidden(true);
//get new path to make sure hidden attribute persisted with file
Path filePath = Paths.get(tempFile.toString());
boolean hidden = Files.isHidden(filePath);
System.out.println("file: " + filePath);
System.out.println("exits: " + Files.exists(filePath));
System.out.println("hidden: " + hidden);
}
}

Output

file: C:\Users\Joe\AppData\Local\Temp\test-file8670368833568014699.txt
exits: true
hidden: true




See Also