Link to home
Start Free TrialLog in
Avatar of ldanet
ldanet

asked on

How to move the directory ?

How to move an directory in an other directory? For example: ../Temp/Test in ../Util/Test

Thanks
Avatar of googlyralph
googlyralph

dont think Java has a move command. Two options as i see it, 1, execute a call to the operating system to perferm the move, E.G

Runtime.execute("move " + source + " " + dest);

or, do a recursive search of the source, and then open, aech file, and write it to the destination directory.

The first is obviosuly not platform indipendant, but is the easiest method, the second is a pure java implemention, but you get to wrestle with recursion and the wonderfull bugs taht all to often creep in.

GR.
ASKER CERTIFIED SOLUTION
Avatar of ozymandias
ozymandias
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Basically you create a file object representing the source directory you want to move and then another file object that would represent the directory after moving. Then you rename the first file to the second file. This moves the directiry and all its sub-directories and files.
BTW, this seems a bit unreliable.

If I use it to "move" a directory on the same drive it works :

    e.g : java MoveDir c:\temp\test c:\tmp\test

However, if I try :

    java MoveDir c:\temp\test d:\temp\test

it doesn't seem to work, but I get no error.
Move commands will work only for local drives (local hard disks)... It will not work for mapped drive, floppy drives... If you execute the dos command you will get some error message. In java also you will get the message if  map process OutputStream and errorStream.

...dviji
Avatar of Mick Barry
I move within a partition is simply a change to the file lut for the filesystem. While a move between partition involves a copy & delete.
This is why moves often don't work across partitions/drives.
Avatar of ldanet

ASKER

Thank you for your answer!