Question

Je dois faire des fichiers et dossiers cachés sur Windows et Linux. Je sais que l'ajout d'un « » à l'avant d'un fichier ou d'un dossier, il sera caché sous Linux. Comment puis-je faire un fichier ou un dossier caché sous Windows?

Était-ce utile?

La solution

Pour Java 6 et ci-dessous,

Vous aurez besoin d'utiliser un appel natif, voici une façon pour les fenêtres

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

Vous devriez apprendre un peu plus sur win32 api-ou Java natif.

Autres conseils

Java 7 peut cacher un fichier DOS ainsi:

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

Un peu plus tôt Java s ne peut pas.

Le code ci-dessus ne lancera pas une exception sur les systèmes de fichiers non-DOS. Si le nom du fichier commence par une période, il sera également caché sur les systèmes de fichiers UNIX.

ce que j'utilise:

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.
}

sur les fenêtres, en utilisant nio java, fichiers

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

Voici un exemple de code Java 7 entièrement compilable qui cache un fichier arbitraire sous Windows.

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());

    }
 } 

Pour vérifier le fichier est caché. Faites un clic droit sur le fichier en question et vous verrez après l'exécution de la cour que le dossier en question est vraiment caché.

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

Utilisez ce code, il pourrait vous résoudre le problème

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top