This is not possible with pure Java. What you want to do is get low-level access to the file system. Different operating systems use very different file systems (Windows: FAT, NTFS; Linux: ext2, ext3, reiserfs, ..., etc.) and all these file systems store data on disk in a different way.
If you want to write a defragmenting program for a specific OS and a specific file system, you'll need to know a lot about the details of that file system, and what the most efficient way is to lay out files on disk for that particular file system. It will not be a trivial program to write.