8

The story behind...

I really like TV shows but I go back home only twice a month. The rest of the time I live in a house without internet (close to my university though, so free wifi rocks! - when it works - ) so I needed a little software that was able to update my portable hard-disk with my new shows when I go back home where the file server synchronized with podcasts does its job. I did it using Java and it works.

The problem

Right now I have a .properties file where I stored the mounted directory of that usb hd. Not enough. I want the software to be able to discover all of USB mass storage devices and let the user select which one use to store files. How can I do that?

Details

a) it has to be in Java (I mean, It could also work with executing local host commands like dir or something like that)

b) my server is on windows, but I prefer it to be an OS independent solution

dierre
  • 7,140
  • 12
  • 75
  • 120

3 Answers3

3

While I didn't see it in your question, I presume you are very familiar with File.listRoots() method which returns an array of well, file roots.

Then, you could just iterate over them, and try to identify if they are flash drives. Some hacks may be like:

File[] roots = File.listRoots();

if (roots == null) {
  // you have a different problem here.  Is it even a computer we are talking about?
}

// Iterate through roots
for (File root : roots) {
    if (root.canWrite()) {  // Or, you could use File.createTempfile with that folder
        // if root does not contain some well know files
        // Or, if root contains some well known files
        // Based on these 2 hacks, or possible other characteristics, you may be reasonably sure
    }
}

That's all I can offer. A lot more can be done with more native programs, and then invoking them from the Java program.

Amrinder Arora
  • 686
  • 7
  • 17
  • Ok, I suppose you're suggesting I should create conditions to check if it's a known device. e.g.: if my root contains `Documents and Settings` directory then I'm on windows xp. Right? – dierre Sep 30 '10 at 17:54
  • Well, to find out the OS is more straight forward (System.getProperty("os.name") etc). I was referring to distinguishing external hard drive from other possible drives (Hard Disk Partition, iPod, CD, DVD etc). – Amrinder Arora Sep 30 '10 at 18:47
  • Well, I had to like your suggestion. I used listRoots for windows and JNI for linux with mount. – dierre Feb 25 '11 at 20:04
2

A beautiful way has been provided in nio. Check this out:

import java.nio.file.*;
for(Path p:FileSystems.getDefault().getRootDirectories()){
    System.out.println(p);
}

You'll get All roots as path objects;

Sufian
  • 6,405
  • 16
  • 66
  • 120
SandeepGodara
  • 1,468
  • 16
  • 12
2

You can check the javax.usb project. Currently there are two certified implementations, for Linux and BSD, and a implementation for windows (as it seems, its still not complete, but I supose it allows you to list the USB devices conected).

But I'm not sure if the posibility of listing only USB drives (instead all drives like in @Amrinder Arora answer) worth adopt a new library set and struggle with a semi-complete implementation...

Tomas Narros
  • 13,390
  • 2
  • 40
  • 56