-2
File[] hiddenFiles = directory.listFiles((FileFilter) HiddenFileFilter.HIDDEN);
for (File hiddenFile : hiddenFiles) {
  String hidden = hiddenFile.getCanonicalPath();
  File file = new File(hidden);
  file.deleteOnExit();
}

I tried this one for deleting a hidden file but this one is not working. Is there any other solution?

Guru
  • 411
  • 3
  • 20
  • 5
    http://importblogkit.com/2015/07/does-not-work/ – Biffen Jan 12 '17 at 10:37
  • 1
    what exactly is not working? does the program not find the hidden file or can it not delete said file? and if I had to guess I would say that you need to change `new File("hiddenfile");` to `new File(hidden);` – Angry Red Panda Jan 12 '17 at 10:37
  • @Guru Are you trying to delete `hiddenFiles`? That code comes nowhere near of achieving that. – Biffen Jan 12 '17 at 10:41
  • Ya i changed. But still not working. – Guru Jan 13 '17 at 06:30
  • @CaptainTreibholz I actually tried by giving the path as static. While uploading i removed the path. That created a confusion. `new File("path\to\hiddenfile");` This is what i actually tried to ask. – Guru Jan 13 '17 at 06:43

2 Answers2

3

There is no difference between deleting a hidden file and a regular file. It should all work well but in your case you are trying to delete file called "hiddenFile". I suggest you remove quotes.

File[] hiddenFiles = directory.listFiles((FileFilter) HiddenFileFilter.HIDDEN);
for (File hiddenFile : hiddenFiles) {
    hidenFile.delete();
    }
}
Witold Kaczurba
  • 9,845
  • 3
  • 58
  • 67
  • I tried this already. I couldn't delete that. When i tried to print the result. The result is false. `System.out.println( hiddenFile.delete());` = false – Guru Jan 13 '17 at 06:38
  • The problem must be somewhere else; perhaps you have no permissions to delete it. I suggest you do a quick test; change your code to use NIO2/Files rather than java.io.File: `try { java.nio.file.Files.delete(hiddenFile.toPath()); } catch (IOException e) { e.printStackTrace(); }` It should allow you to print the error you have. Generally Oracle recommends using NIO2 (Files) rather than File for operations like this. Hope this helps. Some more read about NIO2 and deleting is here: https://docs.oracle.com/javase/tutorial/essential/io/delete.html – Witold Kaczurba Jan 13 '17 at 08:50
  • java.nio.file.DirectoryNotEmptyException: path/to/file at sun.nio.fs.UnixFileSystemProvider.implDelete(UnixFileSystemProvider.java:242) at sun.nio.fs.AbstractFileSystemProvider.delete(AbstractFileSystemProvider.java:103) at java.nio.file.Files.delete(Files.java:1079) at scrm.hidden.main(hidden.java:17) T – Guru Jan 13 '17 at 12:46
  • Looks like you among the files you have a hidden directory that is not empty and you are trying to delete it. You can filter out directories using .isDirectory(). Or write an algorithm that reculsively deletes contents of it. There is good number of online examples how to do that. – Witold Kaczurba Jan 14 '17 at 18:22
0

You should use the path for accessing the file. Don't use File file = new File("hiddenfile"); but use File file = new File(hidden); since that is the directory path of your file.

Casper Vranken
  • 201
  • 3
  • 14