1

I created a file using File() in java in my swing application. How can i make it hidden in my directory.i heard of changing of some attributes but not clear in my beginner perspective. Please help. Thanks in advance.

Hiding is done Thanks for the help. But the read & write on a hidden file is having issue. Please Help. Can the hidden file be treated like that of ordinary file in write/read?

victoryVSK
  • 41
  • 1
  • 3
  • 12

2 Answers2

5

Pure Java 7 way for DOS files:

Path file = Paths.get("fileToHide.dat");
Files.setAttribute(file, "dos:hidden", true);

http://docs.oracle.com/javase/tutorial/essential/io/fileAttr.html

JamesB
  • 7,774
  • 2
  • 22
  • 21
  • is this what you mean: my file is example.xml.----so is this the code: Path file = Paths.get("example.xml"); Files.setAttribute(file, "dos:hidden", true); – victoryVSK Jul 22 '14 at 14:02
  • The get method would need to take the full path to the example.xml file. – JamesB Jul 22 '14 at 14:23
  • Can you help with the issue of reading & writing on ahidden file. I am getting error after hidding my file! – victoryVSK Jul 22 '14 at 14:54
0

It can be done using Java from JDK 1.7 onwards. See How to hide file from Java Program with Code Example using the java.nio.file package:

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);
}
DavidPostill
  • 7,734
  • 9
  • 41
  • 60