Reading a file from CD/DVD.

The java.io has alot of classes for reading and writing. I know I can use the path of my CD/DVD player to make java read from it but if I did not know the path, how can I detemine that during runtime i.e what path my primary cd/dvd device has?

You can’t.

You can make a lame guestimate though:

// Java 6 required
for(File root: File.listRoots())
if(!root.canWrite() && root.getFreeSpace() == 0)
// prolly CD/DVD ?

Thanks, saves me alot of time not trying to find something that does not exist :slight_smile:

I’ve got a feeling that there must be a more concrete way of doing it since the native look and feel JFileChooser can give a cd-icon to cd & dvd drives. Not sure how it does it though. Maybe you can look into the source for the native JFileChooser and figure it out from there???

Keith

Be careful - test this. A lot of drives do weird stuff if you try to access them when they’re not ready, such as blocking Windows Explorer for a minute or more whilst trying to find the disk (or Flash card etc) they think you have in there but you don’t.

Just prompt the user, its the only catch-all solution.

My home pc has 1 cd drive, 1 dvd drive and 4 vitual cd drives - There is no way you can programatically determine the correct one.