Creare un file / cartella nascosta in Windows con Java
Domanda
Ho bisogno di fare file e le cartelle nascoste in Windows e Linux. So che l'aggiunta di un '' alla parte anteriore di un file o di una cartella renderà più nascosta su Linux. Come faccio a fare un file o una cartella nascosta Windows?
Soluzione
Per Java 6 e sotto,
Sarà necessario utilizzare una chiamata nativa, qui è un modo per le finestre
Runtime.getRuntime().exec("attrib +H myHiddenFile.java");
Si dovrebbe imparare un po 'di Win32 API o Java Native.
Altri suggerimenti
La funzionalità che si desidera è una caratteristica di NIO.2 nel prossimo Java 7.
Ecco un articolo che descrive come sarà utilizzato per quello che ti serve: Gestione dei metadati (File Store di file e attributi) . C'è un esempio con file DOS Attributi :
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);
}
attributi impostazione può essere fatto utilizzando DosFileAttributeView
In considerazione di questi fatti, dubito che ci sia un modo standard ed elegante per realizzare questo in Java 6 o Java 5.
Java 7 può nascondere un file DOS in questo modo:
Path path = ...;
Boolean hidden = path.getAttribute("dos:hidden", LinkOption.NOFOLLOW_LINKS);
if (hidden != null && !hidden) {
path.setAttribute("dos:hidden", Boolean.TRUE, LinkOption.NOFOLLOW_LINKS);
}
In precedenza Java-s non è possibile.
Il codice di cui sopra non sarà un'eccezione sul file system non-DOS. Se il nome del file inizia con un punto, allora sarà anche essere nascosto sul file system UNIX.
Questo è quello che uso:
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.
}
In Windows, utilizzando Java nio, i file
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
Ecco un codice di esempio Java 7 completamente compilabile che nasconde un file arbitrario su 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());
}
}
Per controllare il file è nascosto. Fare clic sul file in questione e vedrete dopo l'esecuzione della corte che il file in questione è veramente nascosto.
String cmd1[] = {"attrib","+h",file/folder path};
Runtime.getRuntime().exec(cmd1);
Con questo codice si potrebbe risolvere si problema