Java NIO를 엉망으로 만들고 어떤 이유로 올바른 부울 값을 반환하기 위해 Files.isHidden ()을 얻을 수 없습니다. 프로그램은 디렉토리가 숨겨져 있는지 확인한 다음 숨겨져 있으면 표시되고 숨겨지지 않으면 숨겨집니다. 이것이 내가 가진 것입니다.
Path start = FileSystems.getDefault().getPath("E:/Documents/someDirectory");
try {
if (Files.isHidden(start)){
System.out.println("Dir is hidden.");
Files.setAttribute(start, "dos:hidden", false);
} else {
System.out.println("Dir is not hidden. Hiding.");
Files.setAttribute(start, "dos:hidden", true);
}
} catch (IOException e) {
e.printStackTrace();
}
디렉터리가 숨겨져 있어도 계속 false를 반환하고 디렉터리를 숨 깁니다. 다음 코드는 Path 클래스가있는 이전 File 클래스를 사용하여 제대로 작동합니다.
Path start = FileSystems.getDefault().getPath("E:/Documents/someDirectory");
File file = new File("E:/Documents/someDirectory");
try {
if (file.isHidden()){
System.out.println("Dir is hidden.");
Files.setAttribute(start, "dos:hidden", false);
} else {
System.out.println("Dir is not hidden. Hiding.");
Files.setAttribute(start, "dos:hidden", true);
}
} catch (IOException e) {
e.printStackTrace();
}
의견에서 이미 지적했듯이 Files.isHidden
상태 문서는 다음과 같습니다.
숨김의 정확한 정의는 플랫폼 또는 공급자에 따라 다릅니다. […] Windows에서 파일은 디렉토리가 아니고 DOS 숨김 속성이 설정된 경우 숨김으로 간주됩니다.
마지막 인용 된 문장은 Windows에서 디렉토리에 대한 예상 값을 반환하지 않지만 이미 설명하고 있지만 첫 번째 문장을 강조하고 싶습니다. 플랫폼 / 제공자 특정 시맨틱에 부담이되는 메소드를 사용하고 있지만 원하는 것은 특정 플랫폼 특정 플래그를 토글하는 것입니다.
이 경우 코드의 조건도 제거하여 정확히 수행해야합니다.
Path start=Paths.get("E:/Documents/someDirectory");
boolean isHidden=(Boolean)Files.getAttribute(start, "dos:hidden");
System.out.println("Dir is "+(isHidden? "hidden. Showing.": "not hidden. Hiding"));
Files.setAttribute(start, "dos:hidden", !isHidden);
또한 편리한 메소드 참고 Paths.get(…)
를 들어 FileSystems.getDefault().getPath(…)
.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다