2

I know there are already couple of questions asked but these are not solving my requirements. I need to hide a folder using java. up-till now everything is working fine as I am using Runtime.getRuntime().exec("mv Folder1 .Folder1"); in java but now I am unable to find the solution that how can I hide the folder which has spaces in name.

Note:

  1. moving folders in linux is something different. e.g.

    mv My\ New\ Folder My\ Custom\ Folder

  2. I am using ubuntu.

IConfused
  • 714
  • 2
  • 8
  • 20
  • 1
    Go through this stackoverflow [link][1], it may prove a bit helpful. [1]: http://stackoverflow.com/questions/1999437/how-to-make-a-folder-hidden-using-java – umangm Jun 17 '13 at 16:20

1 Answers1

1

Use File.renameTo.

e.g.

File f = new File("Path to your file");
f.renameTo(".Path to your file");
BillRobertson42
  • 12,602
  • 4
  • 40
  • 57
  • 1
    Well, I am really feeling stupid after seeing such a beautiful answer provided long ago by java community. – IConfused Jun 17 '13 at 16:31
  • 1
    @IConfused: as a rule of thumb: don't use `System.exec()` for things that Java can do on its own. It has *lots* of pitfalls (and correct escaping/passing of arguments is but one of those). – Joachim Sauer Jun 17 '13 at 16:32
  • 3
    @IConfused note the trap: `.renameTo()` does not throw an exception on failure, you have to check for its return value. If you use Java 7, you're better off using `Files.move()` (and ditch the `File` API) – fge Jun 17 '13 at 16:36