The Artima Developer Community
Sponsored Link

Java Buzz Forum
How to hide file from Java Program with Code Example

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Javin Paul

Posts: 1090
Nickname: javinpaul
Registered: Jan, 2012

Javin Paul is Java Programmer working on Finance domain.
How to hide file from Java Program with Code Example Posted: Jan 13, 2012 11:41 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Javin Paul.
Original Post: How to hide file from Java Program with Code Example
Feed Title: Javarevisited Blog
Feed URL: http://javarevisited.blogspot.com/feeds/posts/default?alt=rss
Feed Description: Blog on Java, Unix, Linux, FIX Protocol technology. I share my experience as tutorial, article, interview questions, programming, design etc.
Latest Java Buzz Posts
Latest Java Buzz Posts by Javin Paul
Latest Posts From Javarevisited Blog

Advertisement


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:

Read: How to hide file from Java Program with Code Example

Topic: Judge rules on contested evidence in Oracle-Google case Previous Topic   Next Topic Topic: Mobile developers of the world, unite!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use