Jumat, 28 Maret 2014

How to hide file from Java Program with Code Example




In last article we saw how to check hidden files in Java and now we will see how to hide file in Java or make hidden file in Java. As we said File API in Java doesn’t provide any method to make a file hidden in Java but still you can apply some quick tricks to hide files from Java program. Like in Unix environment any file whose names begin with dot (.) is hidden so you can name your  file starting with dot (.)  and your File will be hidden in Linux or Unix Environment. On Windows you need to directly execute DOS command as stated in how to execute shell commands from Java by using Runtime.getRuntime.exec(“command”) to make the file hidden in Windows. Here is an example of making file hidden in Windows using Java Program.




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








How to make a File hidden from Java Program




Now with JDK7 which introduced Automatic Resource Management , fork join framework  and String in Switch Case , you have a whole new set of File API in java.nio.file package  which provides lots of useful  File and Attributed related functionality like Path which encapsulate Path for any file.





Hide File in Java5 with Example



how to hide file in java program with exampleHere is  complete code example you can use to make a File hidden from Java without using JDK7, disadvantage of this approach is that you are invoking shell command directly from Java code which makes it platform dependent and goes against java primary advantage of write once and read anywhere:









void setHiddenProperty(File file) throws InterruptedException, IOException {

    Process p = Runtime.getRuntime().exec("attrib +H " + file.getPath());

    p.waitFor();

}









here call to p.waitFor() is optional but  calling waitFor() ensures that file will appear hidden as soon as function exit.








Now we will see code example to hide a file in Java7 using new java.nio.file package and new classes like “Path” which encapsulate file path in various operating system and can be used to  operate on files, directories, and other types of files in Java.








Code Example of making a file hidden in JDK7






Here is complete code example of hiding a File in windows from Java7 new features.









Path path = FileSystems.getDefault().getPath("directory", "hidden.txt");

Boolean hidden = path.getAttribute("dos:hidden", LinkOption.NOFOLLOW_LINKS);

if (hidden != null && !hidden) {

    path.setAttribute("dos:hidden", Boolean.TRUE, LinkOption.NOFOLLOW_LINKS);

}












That’s all on how to hide file in both Java5 and Java7 by using either Runtime.exec() or new java.nio.file package introduced in JDK7. let me know if you know any other way of making a file hidden from Java program and we can include that example here.





Thanks





Some other Java Tutorial you may like:



































Source:http://javarevisited.blogspot.com/2012/01/how-to-hide-file-java-program-example.html

Tidak ada komentar:

Posting Komentar