문제

Windows와 Linux에 파일과 폴더를 숨겨야합니다. 나는 '.' 파일이나 폴더의 전면에 Linux에 숨겨집니다. Windows에 파일이나 폴더를 숨기려면 어떻게해야합니까?

도움이 되었습니까?

해결책

Java 6 이하의 경우

기본 전화를 사용해야합니다. 여기 Windows의 한 가지 방법이 있습니다.

Runtime.getRuntime().exec("attrib +H myHiddenFile.java");

Win32-API 또는 Java Native에 대해 조금 배워야합니다.

다른 팁

당신이 원하는 기능은 다가오는 Java 7에서 Nio.2의 특징입니다.

다음은 필요한 것에 어떻게 사용될 것인지 설명하는 기사입니다. 메타 데이터 관리 (파일 및 파일 스토어 속성). 예를 들어 있습니다 DOS 파일 속성:

Path file = ...;
try {
    DosFileAttributes attr = Attributes.readDosFileAttributes(file);
    System.out.println("isReadOnly is " + attr.isReadOnly());
    System.out.println("isHidden is " + attr.isHidden());
    System.out.println("isArchive is " + attr.isArchive());
    System.out.println("isSystem is " + attr.isSystem());
} catch (IOException x) {
    System.err.println("DOS file attributes not supported:" + x);
}

속성 설정을 사용하여 수행 할 수 있습니다 dosfileattributeview

이러한 사실을 고려할 때, Java 6 또는 Java 5에서이를 달성 할 수있는 표준적이고 우아한 방법이 있는지 의심합니다.

Java 7은 다음과 같은 방식으로 DOS 파일을 숨길 수 있습니다.

Path path = ...;
Boolean hidden = path.getAttribute("dos:hidden", LinkOption.NOFOLLOW_LINKS);
if (hidden != null && !hidden) {
    path.setAttribute("dos:hidden", Boolean.TRUE, LinkOption.NOFOLLOW_LINKS);
}

초기 Java-s는 할 수 없습니다.

위의 코드는 비 DOS 파일 시스템에서 예외를 던지지 않습니다. 파일 이름이 기간으로 시작되면 Unix 파일 시스템에도 숨겨집니다.

이것이 제가 사용하는 것입니다.

void hide(File src) throws InterruptedException, IOException {
    // win32 command line variant
    Process p = Runtime.getRuntime().exec("attrib +h " + src.getPath());
    p.waitFor(); // p.waitFor() important, so that the file really appears as hidden immediately after function exit.
}

Windows에서 Java Nio를 사용하여 파일을 사용합니다

Path path = Paths.get(..); //< input target path
Files.write(path, data_byte, StandardOpenOption.CREATE_NEW); //< if file not exist, create 
Files.setAttribute(path, "dos:hidden", Boolean.TRUE, LinkOption.NOFOLLOW_LINKS); //< set hidden attribute

다음은 Windows에서 임의의 파일을 숨기는 완전히 컴파일 가능한 Java 7 코드 샘플입니다.

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.DosFileAttributes;


class A { 
    public static void main(String[] args) throws Exception
    { 
       //locate the full path to the file e.g. c:\a\b\Log.txt
       Path p = Paths.get("c:\\a\\b\\Log.txt");

       //link file to DosFileAttributes
       DosFileAttributes dos = Files.readAttributes(p, DosFileAttributes.class);

       //hide the Log file
       Files.setAttribute(p, "dos:hidden", true);

       System.out.println(dos.isHidden());

    }
 } 

파일을 확인하려면 숨겨져 있습니다. 해당 파일을 마우스 오른쪽 버튼으로 클릭하면 법원을 실행 한 후 해당 파일이 진정으로 숨겨져 있음을 알 수 있습니다.

enter image description here

String cmd1[] = {"attrib","+h",file/folder path};
Runtime.getRuntime().exec(cmd1);

이 코드를 사용하면 문제가 해결 될 수 있습니다

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top